Sunday 15 August 2010

PHP cut text from a specific word in an HTML string -



PHP cut text from a specific word in an HTML string -

i cutting every text ( image alt included ) in html string form specific word.

for illustration string:

<?php $string = '<div><a href="#"><img src="img.jpg" alt="cut text form here" />cut text form here</a></div>'; ?>

and output

<div> <a href="#"> <img src="img.jpg" alt="cut text" /> cutting text </a> </div>

the $string element of object didn't wanted set long code here. can't utilize explode because kill html markup. , str_replace or substr out because length before or after word needs cutting not constant.

so can achive this?

ok solved problem , post reply question because help someone.

so did:

<?php $string = '<div><a href="#"><img src="img.jpg" alt="cut text form here" />cut text form here</a></div>'; $txt_only = strip_tags($string); $explode = explode(' from', $txt_only); $find_txt = array(' from', $explode[1]); $new_str = str_replace($find_txt, '', $string); echo $new_str; ?>

this might not best solution quick , did not involve dom parse. if wants seek create sure href or src or ather attribute needs untouched doesn't have of chars in same way , order in $find_txt else replace too.

php string

No comments:

Post a Comment