Page 1 of 1

What is wrong with my preg_replace URL code?

Posted: Mon Nov 24, 2003 11:21 am
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?

Posted: Mon Nov 24, 2003 3:52 pm
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;

?>

Posted: Tue Nov 25, 2003 2:47 am
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.

Posted: Tue Nov 25, 2003 5:17 am
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....

Posted: Tue Nov 25, 2003 8:38 am
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.

Posted: Tue Nov 25, 2003 8:59 am
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

Posted: Tue Nov 25, 2003 9:51 am
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:

Posted: Tue Nov 25, 2003 10:03 am
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

Posted: Tue Nov 25, 2003 10:09 am
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> 
; 
?>

Posted: Tue Nov 25, 2003 10:20 am
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

Posted: Tue Nov 25, 2003 10:25 am
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.

Posted: Tue Nov 25, 2003 10:40 am
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

Posted: Wed Nov 26, 2003 11:22 am
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