What is wrong with my preg_replace URL code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

What is wrong with my preg_replace URL code?

Post by mesz »

I'm not being rude and trying to force an answer to my question.
Although I have asked this question before I have refined my code myself to the point of almost working...
Now I have just one problem.
The links do not work - they just display as regular text on the screen:
ie ON SCREEN

Code: Select all

їURL=http://www.saua.co.uk]saua.co.ukї/URL]

Code: Select all

<?php
$data = file('./news2/news.dat'); 
$data = array_reverse($data); 
foreach($data as $element) { 
    $element = trim($element); 
    $pieces = explode("|", $element); 
echo $pieces[1] . 
"<BR>" . 
"<b>Posted by " . 
$pieces[3] . 
" on " . 
$pieces[0] . 
"</b> <BR><BR>".
$result = preg_replace('/\\[img\\](.+?)\\[\\/img\\]/', '<img src="\\1">',$pieces[2]); 
$result1 = preg_replace("/\[URL=(.*?)\](.*?)\[\/URL\]/", '<a href="$1">$1</a>',$pieces[1]); 
$result. $result1;
} 
?>
What is wrong with my preg_replace URL code?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

This seems to work..
Notice I'm doing an eregi call just in case someone uses [url] and not [URL]

Code: Select all

<?

$text = eregi_replace("\\[URL=([^\\]]+)\\]([^\\[]+)\\[/URL\\]", "<a href="\\1">\\2</a>", $text);

echo $text;

?>
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Cheers Gen - Ik...I need one other bit of help...
because I have been messing with this same tiny piece of code for 24 hours + I have lost my mind! How the hell do I shoehorn in your snippet of code to mine?
I have tried everything I can think of, but the images still display but not the URLs, my latest attempt is

Code: Select all

<?php
$data = file('./news2/news.dat'); 
$data = array_reverse($data); 
foreach($data as $element) { 
    $element = trim($element); 
    $pieces = explode("|", $element); 
echo $pieces[1] . 
"<BR>" . 
"<b>Posted by " . 
$pieces[3] . 
" on " . 
$pieces[0] . 
"</b> <BR><BR>".
$result = preg_replace('/\\[img\\](.+?)\\[\\/img\\]/', '<img src="\\1">',$pieces[2]); 
$text = eregi_replace("\\[URL=([^\\]]+)\\]([^\\[]+)\\[/URL\\]", "<a href="\\1">\\2</a>", $pieces[1]); 
$result. $text;

} 
?>
I'm not being lazy...I just have mashed this script every single way possible.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

anyone...
I know it will be blindingly simple but I have rearranged every variable I can think of and the URLS do not display. Not even the original text ( the text typed into the form to be turned into a link ) shows. A clue even....
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Code: Select all

<?php

$data = file('./news2/news.dat');

$data = array_reverse($data); 

foreach($data as $element)
{ 
     $element = trim($element); 
     $pieces = explode("|", $element); 

     $pieces[2] = preg_replace('/\\[img\\](.+?)\\[\\/img\\]/', '<img src="\\1">',$pieces[2]); 

     $pieces[1] = eregi_replace("\\[URL=([^\\]]+)\\]([^\\[]+)\\[/URL\\]", "<a href="\\1">\\2</a>", $pieces[1]);

     echo "$pieces[1]<BR><b>Posted by $pieces[3] on $pieces[0]</b><BR><BR>";
}

?>
Not too sure what you're doing but that might work... or it might not.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Just a heads up - if you want to do a case-insensitive preg_replace() you can use an i switch - instead of:

Code: Select all

/\&#1111;URL=(.*?)\](.*?)\&#1111;\/URL\]/
you can use

Code: Select all

/\&#1111;URL=(.*?)\](.*?)\&#1111;\/URL\]/i
The following code worked as expected:

Code: Select all

<?php

$string = '[url=http://www.saua.co.uk]saua.co.uk[/url]';

$formatted = preg_replace('!\[url=(.*?)\](.*?)\[/url\]!i', '<a href="$1">$2</a>', $string);

echo $formatted;
?>
Mac
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Cheers for your help...since I last posted the thread I made changes in order to resolve thsi myself...this has led to other problems that I cannot fathom.

Code: Select all

<?php 
$data = file('./news2/news.dat'); 
$data = array_reverse($data); 
foreach($data as $element) { 
$pieces[1] = preg_replace("/\[url=(.+)\](.+)\[\/url\]/Uim","<a href="\\1">\\2</a>",$pieces[1]); 
    $element = trim($element); 
    $pieces = explode("|", $element); 
echo  "<BR><b>Posted by " . $pieces[4] . " on " . $pieces[0]. "</b><br>".   $pieces[2]. $pieces[1]. 
"<BR><BR>".  
$result1 = preg_replace('/\\[img\\](.+?)\\[\\/img\\]/', '<img src="\\1">',$pieces[3]); 
$result1;

} 
?>
stored as

Code: Select all

11.24.03|&#1111;URL=http://www.saua.co.uk]saua.co.uk&#1111;/URL]|bodytext|&#1111;img]http://www.distortedmedia.co.uk/images/MrBT.gif&#1111;/img]||Lee
The URL prints to screen as it was typed into upload form..including [ ] brackets and the link is unclickable.

t o u c a e n . c o m
- n e w s
Posted by on 11.24.03
bodytextsaua.co.uk

I M A G E DOES PRINT CORRECTLY
I have been trying this for 48 hours+ :cry:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try this code - it worked as expected for me:

Code: Select all

<?php
$data = '11.24.03|[url=http://www.saua.co.uk]saua.co.uk[/url]|bodytext|[img]http://www.distortedmedia.co.uk/images/MrBT.gif[/img]|Lee';

$pieces = explode('|', $data);

$pieces[1] = preg_replace('!\[url=(.+?)\](.+?)\[/url\]!Uim', '<a href="\1">\2</a>', $pieces[1]);
$pieces[3] = preg_replace('!\[img\](.+?)\[/img\]!', '<img src="\1">',$pieces[3]);

echo <<<END
<p>
	<b>Posted By {$pieces[4]} on {$pieces[0]}</b><br />
	{$pieces[2]} {$pieces[1]}<br />
	{$pieces[3]}
</p>
END;

?>
Mac
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

ICheers for the help...but I get this error :? 8O

Code: Select all

Parse error: parse error, expecting `','' or `';'' in /home/virtual/site140/fst/var/www/html/news2/view.php on line 10
Using

Code: Select all

<?php
$data = file('./news2/news.dat'); 
$data = array_reverse($data); 
foreach($data as $element) { 
$pieces = explode('|', $data); 
$pieces[1] = preg_replace('!\[url=(.+?)\](.+?)\[/url\]!Uim', '<a href="\1">\2</a>', $pieces[1]); 
$pieces[3] = preg_replace('!\[img\](.+?)\[/img\]!', '<img src="\1">',$pieces[3]); 

echo 
<p> 
   <b>Posted By {$pieces[4]} on {$pieces[0]}</b><br /> 
   {$pieces[2]} {$pieces[1]}<br /> 
   {$pieces[3]} 
</p> 
; 
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Probably because you change the heredoc format to a plain echo, change:

Code: Select all

echo
<p>
   <b>Posted By {$pieces[4]} on {$pieces[0]}</b><br />
   {$pieces[2]} {$pieces[1]}<br />
   {$pieces[3]}
</p>
;
to

Code: Select all

echo <<<END
<p>
   <b>Posted By {$pieces[4]} on {$pieces[0]}</b><br />
   {$pieces[2]} {$pieces[1]}<br />
   {$pieces[3]}
</p>
END;
For more information on heredoc format:
http://php.net/manual/en/language.types ... ax.heredoc

Mac
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Really sorry to monopolise your time, really sorry but I just cannot sort this...now getting error

Code: Select all

Parse error: parse error, expecting `','' or `';'' in /home/virtual/site140/fst/var/www/html/news2/view.php on line 11
using

Code: Select all

<?php 
$data = file('./news2/news.dat'); 
$data = array_reverse($data); 
foreach($data as $element) { 
$pieces = explode('|', $data); 
$pieces[1] = preg_replace('!\[url=(.+?)\](.+?)\[/url\]!Uim', '<a href="\1">\2</a>', $pieces[1]); 
$pieces[3] = preg_replace('!\[img\](.+?)\[/img\]!', '<img src="\1">',$pieces[3]); 

echo <<<END 
<p> 
   <b>Posted By {$pieces[4]} on {$pieces[0]}</b><br /> 
   {$pieces[2]} {$pieces[1]}<br /> 
   {$pieces[3]} 
</p> 
END;
?>
really, really sorry, and really grateful for your help. This is driving me properly doolalley now.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I copied the code you posted and ran it and didn't get a parse error until I added a space after <<<END - make sure that there are no spaces after it in your code and also that there are no spaces (or tabs) before END;. heredoc can be tricky to get to grips with initally but it does result in nicely formatted HTML code.

Mac
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

sorted it...

Code: Select all

<?php
$data = file('./news2/news.dat');
$data = array_reverse($data);
foreach($data as $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
$pieces[1] = preg_replace('!\[url=(.+?)\](.+?)\[/url\]!Uim', '<a href="\1">\2</a>', $pieces[1]);
$pieces[3] = preg_replace('!\[img\](.+?)\[/img\]!', '<img src="\1">',$pieces[3]);

echo

   "<br><b>posted By ". $pieces[4]. "</b> on  ". $pieces[0]."<br><br>".
   $pieces[2]. "<br><br>". $pieces[1]. "<br ><br>".
   $pieces[3]. "<br><br>";
   }
?>
cheers for all your help...seriously appreciated
Post Reply