0

Possible Duplicate:
PHP: Strip a specific tag from HTML string?
How to parse and process HTML with PHP?

I have a description column on my mysql database which contains some html. like this:

<p><img src="http://www.somesite.co/uploads/Optimized-basil_rajapaksa.jpg" alt="" width="250" height="183" /></p>
<p><span style="font-size: medium; color: #0000ff;">neis,a" iri&uacute; we&yuml;rka leojhs</span></p>
<p><span style="font-size: medium; color: #333333;">&uacute;Yaj&uacute;oHd, wdpd&frac34;hjrekaf.a jD;a;Sh wr.,h i&iuml;nkaOfhka idl&Eacute;Pd ls&Iacute;u ioyd wd&frac34;:sl ixj&frac34;Ok wud;H neis,a rdcmlaI uy;d iy &uacute;Yaj&uacute;oHd, wdpd&frac34;hjrekaf.a i&ntilde;;s i&iuml;f&iuml;,kh w;r fyg ^6&amp; &Egrave;k ;j;a idl&Eacute;Pdjla meje;a&ugrave;ug ;SrKh &ugrave; ;sfnkjd'&uacute;Yaj&uacute;oHd, wdpd&frac34;hjrekaf.a i&ntilde;;s i&iuml;f&iuml;</span><br /><span style="font-size: medium; color: #333333;">wod&lt; idl&Eacute;Pdjg wud;H neis,a rdcmlaI uy;df.ka ks&lt; jYfhka werhqula ,enqK nj o Tyq i|yka l</span></p>

when i echo it on my web page its display the complete html but i want to display only the first span tag value from this whole html on my web page how it can be done?

Community
  • 1
  • 1
Harish Kumar
  • 927
  • 3
  • 20
  • 46

2 Answers2

1

Assuming you have your HTML code into a $html variable:

$html = <<<'CODE'
<p><img src="http://www.somesite.co/uploads/Optimized-basil_rajapaksa.jpg" alt="" width="250" height="183" /></p>
<p><span style="font-size: medium; color: #0000ff;">neis,a" iri&uacute; we&yuml;rka leojhs</span></p>
<p><span style="font-size: medium; color: #333333;">&uacute;Yaj&uacute;oHd, wdpd&frac34;hjrekaf.a jD;a;Sh wr.,h i&iuml;nkaOfhka idl&Eacute;Pd ls&Iacute;u ioyd wd&frac34;:sl ixj&frac34;Ok wud;H neis,a rdcmlaI uy;d iy &uacute;Yaj&uacute;oHd, wdpd&frac34;hjrekaf.a i&ntilde;;s i&iuml;f&iuml;,kh w;r fyg ^6&amp; &Egrave;k ;j;a idl&Eacute;Pdjla meje;a&ugrave;ug ;SrKh &ugrave; ;sfnkjd'&uacute;Yaj&uacute;oHd, wdpd&frac34;hjrekaf.a i&ntilde;;s i&iuml;f&iuml;</span><br /><span style="font-size: medium; color: #333333;">wod&lt; idl&Eacute;Pdjg wud;H neis,a rdcmlaI uy;df.ka ks&lt; jYfhka werhqula ,enqK nj o Tyq i|yka l</span></p>
CODE;

You can do this:

preg_match_all('/\<span[^\>]*\>([^\<]*)\<\/span\>/', $html, $matches);
echo $matches[1][0]; // Shows: neis,a" iri&uacute; we&yuml;rka leojhs

And if you want the content of your second <span> just do:

echo $matches[1][1];

And so on in the case you had more <span> tags.

Carlos
  • 4,949
  • 2
  • 20
  • 37
  • i am putting the html using fetching array like this $html = <<<'code' $fetch["content"] code; it shows me the error – Harish Kumar Oct 22 '12 at 08:18
  • @jackflash: Better point to existing questions that are covering this much better. Just noting, you can place your answer but this is a duplicate candidate. – hakre Oct 22 '12 at 08:20
  • @hakre All right. I didn't realize this could be duplicate question. Just answered in order to help OP. – Carlos Oct 22 '12 at 08:23
  • @HarishKumar Just do this: `$html = $fetch['content'];` – Carlos Oct 22 '12 at 08:24
  • @jackflash your comment did my job but if i want to display the second span tag values then what should i do? – Harish Kumar Oct 22 '12 at 08:27
  • @HarishKumar As I said in my answer, just this: `echo $matches[1][1];` – Carlos Oct 22 '12 at 08:28
  • @jackflash Don't parse HTML with regex. Use a Dom parser such as DomDocument. – Wayne Whitty Oct 22 '12 at 08:29
  • @WayneWhitty In this case it is much faster to use regex. – Carlos Oct 22 '12 at 08:34
  • @jackflash Nope. In the future, he might want to find the first link or the second span or the fifth paragraph and that regex wont help him. This will lead to more regex and before you know it, it's one big unmaintainable mess. HTML is too nuanced to tackle with regex. – Wayne Whitty Oct 22 '12 at 08:37
  • @WayneWhitty I agree. This is a solution for this current problem. If his future needs or code change he will need another solution that fits the changes. But if he thinks this is never happening, why should he kill a fly with a cannon? – Carlos Oct 22 '12 at 08:51
0

You should look into using DomDocument. It'll allow you to parse HTML with ease.

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
  • I suggest to leave that as a comment instead of an answer. – hakre Oct 22 '12 at 08:15
  • @hakre Why? If he took the time to look at DomDocument, he'd discover that it would solve his problem. Much better than providing him with regex examples. – Wayne Whitty Oct 22 '12 at 08:32
  • Because it's more a comment than an answer. I didn't say that this would be technically wrong btw.. I think you did suggest something very useful here actually. – hakre Oct 22 '12 at 08:33
  • Point taken. Maybe I should have provided some DomDocument examples as well. – Wayne Whitty Oct 22 '12 at 08:34
  • Well, I'd say that's not really necessary as this website has these already. It might be more useful for this FAQ-Site to point to an exact duplicate of this question (the one you think that does this well with DOMDocument for example). – hakre Oct 22 '12 at 08:35