CodeIgniter Forums

Full Version: Reg Exp (not strict CI)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]abmcr[/eluser]
I want to extract with a reg exp the img tag in a text for the img width src = *images/rullino.png
This is an example of the text
Code:
<p><img title="prova " src="/CmsAB1/images/rullino.png" alt="5" /> <br />Il presidente del Consiglio critica .....<img title="ert" src="/CmsAB1/uploads/images/Image/.thumb_PICT0119.jpg" alt="ter" width="96" height="96" />.....</p>

I have try with
Code:
$pattern = '#\<img  src="[^\}]*/images/rullino.png"/>#i';
and this partial work because extract the tag of the img but all after....

It is possible to extract ONLY the alt tag for the images with src = *images/rullino.png?
Thank you

El Forum

[eluser]TheFuzzy0ne[/eluser]
How many images do you need to match? Just one? Also, are they guaranteed to be in XHTML syntax, and not HTML?

Also, please give an example of the format you'd expect to receive the results back in. For example, an array containing the alt value. If this is the case, do you need anything else in that array? Sorry, but I don't understand how this is going to be used, so it's hard to guess.

El Forum

[eluser]pistolPete[/eluser]
You could try the following:
Code:
$text = '<p><img title="prova " src="/CmsAB1/images/rullino.png" alt="5" /> <br />Il presidente del Consiglio critica .....<img title="ert" src="/CmsAB1/uploads/images/Image/.thumb_PICT0119.jpg" alt="ter" width="96" height="96" />.....</p>';
$pattern = '&<img.*src\=\".*\/images\/rullino.png\".*alt=\"(.+)\".*>&isU;';
preg_match_all($pattern, $text, $subpattern);

foreach ($subpattern[1] as $match)
{
    echo $match.'<br />';
}

If the text is valid HTML, you could try a HTML parser such as simplehtmldom.

El Forum

[eluser]TheFuzzy0ne[/eluser]
PHP also supports DOM traversal and manipulation - http://uk.php.net/book.dom

El Forum

[eluser]abmcr[/eluser]
TheFuzzy0ne: this script replace a placeholder in a text with a set of image

[quote author="pistolPete" date="1241373944"]You could try the following:
Code:
$text = '<p><img title="prova " src="/CmsAB1/images/rullino.png" alt="5" /> <br />Il presidente del Consiglio critica .....<img title="ert" src="/CmsAB1/uploads/images/Image/.thumb_PICT0119.jpg" alt="ter" width="96" height="96" />.....</p>';
$pattern = '&<img.*src\=\".*\/images\/rullino.png\".*alt=\"(.+)\".*>&isU;';
preg_match_all($pattern, $text, $subpattern);

foreach ($subpattern[1] as $match)
{
    echo $match.'<br />';
}
[/quote]
Thank you: this work fine.
And i take advantage for another similar question:
for get the entire img tag and not only the tag what i do? the pattern
Code:
$pattern = '&<img.*src\=\".*\/images\/rullino.png\".*alt=\"(.+)\".*>&isU;';
extract the alt tag, but i need also the entire tag , i.e:
Code:
<img title="prova " src="/CmsAB1/images/rullino.png" alt="5" />
Thank you in advance.

El Forum

[eluser]pistolPete[/eluser]
[quote author="abmcr" date="1241385660"]... but i need also the entire tag[/quote]

Have a look at the $subpattern array, the full tag is stored in $subpattern[0]:

Code:
...
echo '<pre>'.print_r($subpattern,TRUE).'</pre>';

El Forum

[eluser]abmcr[/eluser]
Thank you
i apologize for my ignorance about regexp

El Forum

[eluser]TheFuzzy0ne[/eluser]
I don't consider it ignorance, and I'm sure I speak for Pete, also. I've been playing with Regular Expressions on and off for years now, and I still don't get it...

El Forum

[eluser]abmcr[/eluser]
[quote author="TheFuzzy0ne" date="1241460140"]I don't consider it ignorance, and I'm sure I speak for Pete, also. I've been playing with Regular Expressions on and off for years now, and I still don't get it...[/quote]
Thank you: my english is much italian and ignorance is not the correct word ;-)