Noodle Head
Noodle Head

Reputation: 431

RegEx Not Working As Expected When Put Into Preg_Replace

I want to remove the following - title=\"huluId-581956\" - from a string so that:

<a title=\"huluId-581956\" href="somelink">My Link</a>

becomes

<a href="somelink">My Link</a>

I'm basically looking to take the title attribute out. I finessed my exp on regexpal and put it into preg_replace as such:

$myString ='<a title=\"huluId-581956\" href="somelink">My Link</a>';

$myString = preg_replace('/(title=\\)("huluId-)[0-9]+\\(")/', '', $myString);
$myString = preg_replace('/(title=\\)("huluId-)[0-9]+(\\")/', '', $myString);

But although on regexpal I have no problem selecting the title attribute, when I place the expression into preg_replace it does NOT work.

Any help would be greatly appreciated as I have no idea why this would be so.

Thank you!

Upvotes: 0

Views: 59

Answers (3)

Patrick Bard
Patrick Bard

Reputation: 1842

Considering that you will normally have the slash after "title" you can have a simpler regex:

/title=\\"(.)*?"/ 

It selects everything after 'title=\"', and the "?" make it ends on the next character, which is a quotation mark.

The code:

$myString ='<a title=\"huluId-581956\" href="somelink">My Link</a>';

$myString = preg_replace('/title=\\"(.)*?"/', '', $myString);

Upvotes: 0

Enissay
Enissay

Reputation: 4953

Simply use this instead:

$myString = preg_replace('/\s+title=\\\\"[^"]+"/', '', $html);

Also, since I don't know in what context you're using this, maybe consider using a DOM parser because regex is not the appropriate tool for HTML parsing... A DOM parser like PHP Simple HTML DOM Parser can do that easily...

Working DEMO

Upvotes: 2

mikevoermans
mikevoermans

Reputation: 4007

The slashes are messing up the regex - strip them out and it makes life easier.

$myString ='<a title=\"huluId-581956" href="somelink">My Link</a>';
$myString = stripslashes($myString);
$myString = preg_replace('/title="huluId-[0-9]+" /', '', $myString);
echo $myString;

Upvotes: 0

Related Questions