BobDylan
BobDylan

Reputation: 15

Regex PHP remove certain keyword

After viewing some answers on stackoverflow,

preg_match_all('/<img[^>]+>/i',$html, $result);
$img = array();
foreach( $result[0] as $img_tag)
{
    preg_match_all('/(title)=("[^"]*")/i',$img_tag, $img[$img_tag]);
}

//print_r($img);
foreach ($img as $imgg)
 echo $imgg[2][0];

The above code finds img title, but however it return as "Waterfall fountain" instead of Waterfall fountain, notice there is "

what should i add in regex to remove "?

Thank you

Upvotes: 0

Views: 168

Answers (6)

Antarctica Ant
Antarctica Ant

Reputation: 11

Move the quotes outside of your brackets.

Check this :

preg_match_all('/(title)="([^"]*)"/i',$img_tag, $img[$img_tag]); 

Upvotes: 1

Gordon
Gordon

Reputation: 316969

Use an XML Parser and this XPath to get all titles of img elements:

//img/@title

Example with DOM

$dom = new DOMDocument;
$dom->loadHML($html);
$xp = new DOMXPath($dom);
foreach($xp->query('//img/@title') as $attribute) {
    echo $attribute->nodeValue;
}

Further readings:

Upvotes: 1

Michael Mrozek
Michael Mrozek

Reputation: 175365

Parentheses in a regular expression make a capturing group, which control what get stored in $img[$img_tag]. Your group included the quotes: ("[^"]*"). If you don't want the quotes, just move them outside the group: "([^"]*)"

Upvotes: 0

codaddict
codaddict

Reputation: 455000

Currently you are making the " part of the match that is remembered. You can put the quotes outside the parenthesis:

preg_match_all('/(title)="([^"]*)"/i',$img_tag, $img[$img_tag]);

Upvotes: 0

Steven
Steven

Reputation: 18014

Just move the " out of the capturing group:

'/(title)="([^"]*)"/i'

Upvotes: 3

Mark Baker
Mark Baker

Reputation: 212412

move the quotes outside of your brackets

preg_match_all('/(title)="([^"]*)"/i',$img_tag, $img[$img_tag]); 

Upvotes: 1

Related Questions