Jump to content


Photo

eregi_replace () ;


  • Please log in to reply
3 replies to this topic

#1 Darkness Soul

Darkness Soul
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts
  • LocationBrazil; São Paulo.

Posted 14 August 2006 - 08:38 PM

Yo,

I need a little help.. I'm supose to read from a file an url and show it as well.. like it:
:BOF:
[http://www.phpfreaks.com/]
:EOF:

So, I need to remove these [ and ] to build something like:
<a href="http://www.phpfreaks.com/">http://www.phpfreaks.com/</a>

I've tried eregi_replace, but I'm not good with regular expressions yet.. :(

Thanks for any help..

D.Soul
(If something is wrong, please tell me. I'm learning this language. Thank you)

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 14 August 2006 - 09:19 PM

$str = '[http://www.phpfreaks.com/]';
$str = trim($str, '[]');
echo '<a href="' . $str . '">' . $str . '</a>';


#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 14 August 2006 - 09:23 PM

This will work:

<?php

$contents = ':BOF: [http://www.phpfreaks.com/] :EOF:';
preg_match("/(http:\/\/)[a-zA-Z0-9-]+[\._a-zA-Z0-9-]+/i", $contents, $links);

echo"<a href=\"$links[0]\">$links[0]</a>";

?>

PHP Ninja

#4 Darkness Soul

Darkness Soul
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts
  • LocationBrazil; São Paulo.

Posted 15 August 2006 - 12:17 PM

Yo,

$str = '[http://www.phpfreaks.com/]';
$str = trim($str, '[]');
echo '<a href="' . $str . '">' . $str . '</a>';

Yeah, but this isn't the way i need it..

This will work:

<?php

$contents = ':BOF: [http://www.phpfreaks.com/] :EOF:';
preg_match("/(http:\/\/)[a-zA-Z0-9-]+[\._a-zA-Z0-9-]+/i", $contents, $links);

echo"<a href=\"$links[0]\">$links[0]</a>";

?>

This help too, but what i need is to replace that url inside a variable with the text line, so, when I print that line, its print the link as well.. and not the url alone..
=)
an expemple:
$var = 'this is the text line with [http://www.test.com] or http://www.test.com url inside' ;
the trim method don't leave to print this line with the second link.. and the is a better way to me to keep the url safe.. so, i think about use preg_replace, based on your post, caesar..
Bye,
D.Soul
(If something is wrong, please tell me. I'm learning this language. Thank you)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users