help to debug a simple php preg_replace [message #172056] |
Fri, 28 January 2011 17:40 |
juliani(dot)moon(at)gmail(dot)co
Messages: 6 Registered: January 2011
Karma: 0
|
Junior Member |
|
|
I have following php code to convert "<" to "<" 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('/</', '<', $textline);
#$hyprtext = preg_replace('/>/', '>', $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 |
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('/</', '<', $textline);
$hyprtext = preg_replace('/>/', '>', $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 |
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('/</', '<', $textline);
> $hyprtext = preg_replace('/>/', '>', $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 #172060 is a reply to message #172059] |
Fri, 28 January 2011 19:36 |
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 |
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 |
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('/</', '<', $textline);
> $hyprtext = preg_replace('/>/', '>', $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(
'<',
'>',
'_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
|
|
|
|