PHP cut text from a specific word in an HTML string -
I would like to deduct every text (include image alt) in a HTML string as a specific word.
For example, this is the string:
& lt ;? Php $ string = '& lt; Div & gt; & Lt; A href = "#" & gt; & Lt; Img src = "img.jpg" alt = "cut this text form here" /> Cut this text form here & lt; / A & gt; & Lt; / Div & gt; '; ? & Gt;
And this is what I would like to output
& lt; Div & gt; & Lt; A href = "#" & gt; & Lt; Img src = "img.jpg" alt = "cut this text" /> This text cuts & lt; / A & gt; & Lt; / Div & gt; Actually there is an element of an object, but I did not want to keep a long code here, obviously I can not use the explosion because it's HTML Markup will kill. And also str_replace or substr is out because the term is not constant before or after the length where it needs to cut. So what can I do to get it?
OK, I resolve my problem and I only post an answer to my question because It can help someone. / P>
So this is what I did:
& lt ;? Php $ string = '& lt; Div & gt; & Lt; A href = "#" & gt; & Lt; Img src = "Img.jpg" alt = "Cut this text form here" /> Cut this text form here & lt; / A & gt; & Lt; / Div & gt; '; $ Txt_only = strip_tags ($ string); $ Explosion = explosion ('from', $ txt_only); $ Find_txt = array ('from', $ explosion [1]); $ New_str = str_replace ($ find_txt, '', $ string); Resonance $ new_str; ? & Gt;
This may not be the best solution but it was quick and did not include Dome Pars. If someone wants to try it, make sure that your href
or src
or any attribute should not be untouched, in the same way and in no order As is the case with $ find_txt
and it will replace those people.
Comments
Post a Comment