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
Switch to threaded view of this topic Create a new topic Submit Reply
help to debug a simple php preg_replace [message #172056] Fri, 28 January 2011 17:40 Go to next message
juliani(dot)moon(at)gmail(dot)co is currently offline  juliani(dot)moon(at)gmail(dot)co
Messages: 6
Registered: January 2011
Karma: 0
Junior Member
I have following php code to convert "<" to "&#60;" and "@" to "_at_",
etc. However it does not work (no effects - text are all printed as
they are) on command line test:

<?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";
}
?>

Could some sharp eyes help to see what's wrong?

Joe
Re: help to debug a simple php preg_replace [message #172057 is a reply to message #172056] Fri, 28 January 2011 17:43 Go to previous messageGo to next message
juliani(dot)moon(at)gmail(dot)co is currently offline  juliani(dot)moon(at)gmail(dot)co
Messages: 6
Registered: January 2011
Karma: 0
Junior Member
Ooops, the copy-paste was a mess. Now it is again:

<?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";}
?>

Note: The line for "http" works fine.
Re: help to debug a simple php preg_replace [message #172058 is a reply to message #172057] Fri, 28 January 2011 18:01 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 28-01-11 18:43, juliani(dot)moon(at)gmail(dot)com wrote:
> Ooops, the copy-paste was a mess. Now it is again:
>
> <?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";}
> ?>
>
> Note: The line for "http" works fine.

read the docs again... ;)

http://php.net/manual/en/function.preg-replace.php

--
Luuk
Re: help to debug a simple php preg_replace [message #172059 is a reply to message #172058] Fri, 28 January 2011 18:46 Go to previous messageGo to next message
juliani(dot)moon(at)gmail(dot)co is currently offline  juliani(dot)moon(at)gmail(dot)co
Messages: 6
Registered: January 2011
Karma: 0
Junior Member
I found it - it's not regular expression or preg_replace construct,
but my logic problem (multiple $hyprtext lines result in only the last
one taking effect).

joe
Re: help to debug a simple php preg_replace [message #172060 is a reply to message #172059] Fri, 28 January 2011 19:36 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 28-01-11 19:46, Joe wrote:
> I found it - it's not regular expression or preg_replace construct,
> but my logic problem (multiple $hyprtext lines result in only the last
> one taking effect).
>
> joe

indeed,

so replate $hyprtext with $textline everywhere in your (previously
posted) code,
and you will be fine..

--
Luuk
Re: help to debug a simple php preg_replace [message #172062 is a reply to message #172059] Fri, 28 January 2011 23:03 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 28/01/11 18:46, Joe wrote:
> I found it - it's not regular expression or preg_replace construct,
> but my logic problem (multiple $hyprtext lines result in only the last
> one taking effect).

If you want to apply multiple preg_replaces one after the other, do the
following (and pay close attention to the var names used here):

<?php

$txt = preg_replace("/needle1/","replace1",$txt);
$txt = preg_replace("/needle2/","replace2",$txt);
$txt = preg_replace("/needle3/","replace3",$txt);
$txt = preg_replace("/needle4/","replace4",$txt);

?>

If you do what you did:

<?php

$txt2 = preg_replace("/needle1/","replace1",$txt);
$txt2 = preg_replace("/needle2/","replace2",$txt);
$txt2 = preg_replace("/needle3/","replace3",$txt);
$txt2 = preg_replace("/needle4/","replace4",$txt);

?>

Then you might as well just do this instead:

<?php

$txt2 = preg_replace("/needle4/","replace4",$txt);

?>

Rgds

Denis McMahon
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 next message
Helmut Chang is currently offline  Helmut Chang
Messages: 22
Registered: September 2010
Karma: 0
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
Re: help to debug a simple php preg_replace [message #172173 is a reply to message #172073] Thu, 03 February 2011 04:16 Go to previous message
juliani(dot)moon(at)gmail(dot)co is currently offline  juliani(dot)moon(at)gmail(dot)co
Messages: 6
Registered: January 2011
Karma: 0
Junior Member
Thank you everyone for taking time to reply. This is indeed
educational.

joe
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Shri Shraddha Astrology
Next Topic: Asynchronous FTP Upload
Goto Forum:
  

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

Current Time: Fri Nov 22 15:19:06 GMT 2024

Total time taken to generate the page: 0.02244 seconds