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
|
|
0
|
|
|
|
Reply
|
juliani.moon (9)
|
1/28/2011 5:40:01 PM |
|
Ooops, the copy-paste was a mess. Now it is again:
<?php
if (file_exists("infile")) {
=A0$textline =3D file_get_contents("infile");
$hyprtext =3D preg_replace('/</', '<', $textline);
$hyprtext =3D preg_replace('/>/', '>', $textline);
=A0$hyprtext =3D preg_replace('/@/', '_at_', $textline);
=A0$hyprtext =3D preg_replace("/(http:\/\/[^\s]+)/", "<a href=3D\"$1\">$1<=
/
a>", $textline);
=A0print "$hyprtext";}
?>
Note: The line for "http" works fine.
|
|
0
|
|
|
|
Reply
|
juliani
|
1/28/2011 5:43:15 PM
|
|
On 28-01-11 18:43, juliani.moon@gmail.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
|
|
0
|
|
|
|
Reply
|
Luuk
|
1/28/2011 6:01:27 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
Joe
|
1/28/2011 6:46:20 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
Luuk
|
1/28/2011 7:36:19 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
Denis
|
1/28/2011 11:03:54 PM
|
|
Am 28.01.2011 18:43, schrieb juliani.moon@gmail.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
|
|
0
|
|
|
|
Reply
|
Helmut
|
1/29/2011 3:20:38 PM
|
|
Thank you everyone for taking time to reply. This is indeed
educational.
joe
|
|
0
|
|
|
|
Reply
|
Joe
|
2/3/2011 4:16:40 AM
|
|
|
7 Replies
111 Views
(page loaded in 0.114 seconds)
|