Jump to content

[SOLVED] Truncating a string after a found string inside of that string.


jlange

Recommended Posts

I'm not sure how to do this. What I'm trying to do is this:

 

turn this:

<a href="somelink"><img src="imgsrc" height="y" width="x">Some nonstatic text, courtesy of Flickr</a>

 

into this:

<a href="somelink"><img src="imgsrc" style="mystyle"></a>

 

thoughts?

by the way, i figured i can use a string search function to find a value from an array i'll make that will have all the different kinds of image file extensions, so, is there a string function to find what character number that is, or something? then, i could truncate it after that number of characters then just add on my string to the end.

regular expression is the answer.

$str = <a href="somelink"><img src="imgsrc" height="y" width="x">Some nonstatic text, courtesy of Flickr</a>';
$str = preg_replace('/(<a(.)*?>)<img(.)*?>(.)*?<\/a>/','$1$3</a>',$str);

 

try that..

$stringpos = strpos($content, '.jpg');
    		   	$stringpos = $stringpos+4;
    		   	$chars = $stringpos;
    		    $content = substr($content,0,$stringpos);
    		    $content = $content."\" style=\"height:100px\" /></a>";

 

works! thanks!

sorry didn't etst it out first!!!!

 

try this...

$str = '<a href="somelink"><img src="imgsrc" height="y" width="x">Some nonstatic text, courtesy of Flickr</a>';
$str = preg_replace('/(<a(.)*?><img(.)*?>)(.)*?<\/a>/','$1</a>',$str);
echo $str;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.