Web Hosting Talk







View Full Version : how to get part of string with PHP?


orbitz
04-15-2004, 12:37 AM
hi,

if i have this string:

$original_str = "<img src='http://localhost/images/1.gif' border='0' alt='user posted image' /><br><br>testing images";

I have 2 questions:

1- how to get the link to the image from the above string
2- after I have determined the image's dimesion (i can do this part),
how do I add the width and length back to the string above?

How do i get the link from the above string?
the result should be:

'http://localhost/images/1.gif'


The reason i need to get this link out is to determine the dimension of the image and then add the width and height back to the original above:

suppose that:
$mylink = 'http://localhost/images/1.gif'
//
$img_sz = getimagesize($mylink);
$width = $img_sz[0];
$height = $img_sz[1];

// from here, how do i add the width and height back to the $oringial_str ?

Thanks for your help

orbitz
04-15-2004, 01:36 AM
Never mind,

I have done it.

I am gonna post them here just in case someone else will ask the same question:

This is gonna be for a forum mod on my invision board

<?
$text = "<img src='http://localhost/images/1.gif' border='0' alt='user posted image'/><br><br>toi testing images";

// get image link from tag image
preg_match( "/<img src=[\"'](.+?)[\"']/", $text, $matches);
echo"<pre>";
echo $matches[1]; // this print out the link to the image
echo"</pre>";

//get the width and height of the image
$img_sz = getimagesize($matches[1]);
$width = $img_sz[0];
echo $width;
echo "<br>";
$height= $img_sz[1];
echo $height;
echo "<br>";

// if the width is bigger than 400
//set the width to 400 when displaying on the forums
// to view original size, click on the image.
if ($width > 400);
{
$text = preg_replace( "/<img src=[\"'](.+?)[\"'].+?".">/", "<a href='\\1' target='_blank'><img src='\\1' width=400></a>", $text);
}
echo $text;
?>