Regex PHP remove certain keyword

852

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 ofWaterfall fountain, notice there is"

what should i add in regex to remove"?

Thank you

17

Answer

Solution:

Just move the" out of the capturing group:

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

Answer

Solution:

move the quotes outside of your brackets

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

Answer

Solution:

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:

589

Answer

Solution:

Move the quotes outside of your brackets.

Check this :

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

Answer

Solution:

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]);
327

Answer

Solution:

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:"([^"]*)"

People are also looking for solutions to the problem: php - How do I directly upload a file stream to Flickr using a CURL POST?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.