Page 1 of 1

simple simple preg question

Posted: Mon Nov 24, 2003 7:53 am
by mesz
Help....
I have this code:

Code: Select all

<?php
$data = file('./news2/news.dat'); 
$data = array_reverse($data); 
foreach($data as $element) { 
$result = preg_replace('/\\[img\\](.+?)\\[\\/img\\]/', '<img src="\\1">',$pieces[2]); 
    $element = trim($element); 
    $pieces = explode("|", $element); 
echo $pieces[2] . 
"<BR>" . 
"<b>Posted by " . 
$pieces[1] . 
" on " . 
$pieces[0] . 
"</b> <BR><BR>". 
$result ; 
} 
?>
It displays images ( sometimes...)...how can I also add in a facility for displaying URLs that are clickable...
I thought:

Code: Select all

$result1 = preg_replace('/\\&#1111;url=(.*?)\\](.*?)\\&#1111;\\/url\\]/', '&lt;a href="\\1"&gt;&lt;\/a&gt;',$pieces&#1111;2]);

but it does not work

Posted: Mon Nov 24, 2003 8:06 am
by patrikG
Try

Code: Select all

<?php
$result = preg_replace("/\[url=(.+)\](.+)\[\/url\]/Uim","<a href="\\1">\\2</a>",$pieces[2]);
?>

Posted: Mon Nov 24, 2003 8:12 am
by mesz
Cheers..that displays teh links...and they work...but not the images!

Code: Select all

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

Posted: Mon Nov 24, 2003 8:23 am
by mesz
All works...now...I need some more complicated help if that is alright.

Why does the original text display as well.

Code: Select all

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

Posted: Mon Nov 24, 2003 8:56 am
by mesz
Trying to not cross post.
The question is also being looked at here
http://www.devnetwork.net/forums/viewtopic.php?t=14828