NoReceipt4Panda NoReceipt4Panda - 6 months ago 16
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="http://example.com/myPDF.pdf" 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

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.