FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » help to debug a simple php preg_replace
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: help to debug a simple php preg_replace [message #172073 is a reply to message #172057] Sat, 29 January 2011 15:20 Go to previous messageGo to previous message
Helmut Chang is currently offline  Helmut Chang
Messages: 22
Registered: September 2010
Karma:
Junior Member
Am 28.01.2011 18:43, schrieb juliani(dot)moon(at)gmail(dot)com:

> <?php
> if (file_exists("infile")) {
> $textline = file_get_contents("infile");
> $hyprtext = preg_replace('/</', '&#60;', $textline);
> $hyprtext = preg_replace('/>/', '&#62;', $textline);
> $hyprtext = preg_replace('/@/', '_at_', $textline);
> $hyprtext = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</
> a>", $textline);
> print "$hyprtext";}
> ?>

Besides the already posted solutions to your problem, here two more
suggestions:

1. preg_replace can also work with pattern/replacement arrays:

$patterns = array(
'/</',
'/>/',
'/@/',
"/(http:\/\/[^\s]+)/"
);

$replacements = array(
'&#60;',
'&#62;',
'_at_',
"<a href=\"$1\">$1</a>"
);

$hyprtext = preg_replace($patterns, $replacements, $textline);

2. Except the last pattern, the other three could also easily be done
with a "simple" str_replace:

<http://www.php.net/manual/en/function.str-replace.php>

Helmut
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Shri Shraddha Astrology
Next Topic: Asynchronous FTP Upload
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 04:18:57 GMT 2024

Total time taken to generate the page: 0.08178 seconds