NoReceipt4Panda NoReceipt4Panda - 1 year ago 78
PHP Question

What is the correct way to str_replace until the end of string in PHP?

If I have a string like this:

<a href="" rel="">myPDF12345431234</a>

what would be the proper PHP function call to str_replace so that everything after " " rel=.... " is replaced with nothing/blank?

For further clarity, The value in between the link tags is dynamic and unknown, so I can't call it.

I'm looking for something like:

$oldstring = array('<a href="', '" rel="" *until the end_of_string* ');
$replacewith = array=('', '');

$newstring = str_replace($oldstring, $replacewith, $URL);

What is the proper way to get everything after the URL (quotation mark til the end of the string) to be replaced with nothing?

Answer Source

This is handled by regular expressions. The following replaces everything from rel="" to the end of the string.

$newstring = preg_replace('/ rel="".*$/','',$oldstring);

The .* means "everything" and $ means "end of string". I added a space before rel because I assume you want to drop that as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download