1 (edited by jochem 2004-03-23 08:07)

Topic: replace part of string.

if i have a string like this

"this is some text and this [img]http://url_to_image[/img] is an image".

how can i replace the part

"[img]http://url_to_image[/img]"

with
<a href='http://url_to_image' target='_blank' border='0'><img src='http://url_to_image' width='100'></a>"
or
<a href='http://url_to_image' target='_blank' border='0'><img src='http://url_to_image' height='100'></a>"

if the width is bigger then the height, i want to put width='100',
if the height is bigger then the width, i want to put height='100',
i know i can do that with getimagesize

i tried something like this to determine whether i have to use width or height:

$image = 'http://localhost/users/images/top.jpg';
list($w, $h) = getimagesize($image);

echo "<a href=".$image." target='_blank'><img src=".$image." border='0' ";
if ($w > $h) {
 echo "width='100'>";
}
if ($h > $w) {
 echo "height='100'>";
}
echo "</a>";

i know this works...i just don't know how to replace that part of the string..(like what i described above.

thank you

2

Re: replace part of string.

  $string = "http://static.php.net/www.php.net/images/php.gif";
  echo $string . " = " . urlencode($string) . "<br /><br />\n";

  list($width, $height, $type, $attr) = getimagesize($string);
  echo "<img src=\"" . $string . "\" $attr alt=\"getimagesize() example\" />   width = " . $width . "   height = " . $height;
  echo "<b>resized:</b> <img src=\"" . $string . "\" ";
  if($width >= $height) {
     echo "width=\"100\"";
  } elseif($width < $height) {
     echo "height=\"100\"";
  }
  echo ">";

3 (edited by 7.3. 2004-03-24 19:12)

Re: replace part of string.

<?php
$text = "this is some text and this [img]http://url_to_image[/img] is an image";
$text = ereg_replace("\\[img\\](http://[-~a-zA-Z_0-9/.+;%&?|=:]+)\\[/img\\]",
"<a href='\\1' target='_blank' border='0'><img src='\\1' width='100'></a>",
$text);
?>

Re: replace part of string.

thank you both...
but now, how do i get the url out of the string to do a getimagesize on it?

5

Re: replace part of string.

I dunno (-;

Re: replace part of string.

you can get the url from $HTTP_SERVER_VARS['REQUEST_URI'] /$_SERVER['REQUEST_URI'].

if you want to get the url inside the code, then you have to break that page into array (read as lines), then search with regular expression. then you can easily change whatever you want.

anything else please do quest! smile

God wisely designed the human body so that we can neither pat our own backs nor kick ourselves too easily