simple simple preg question

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

simple simple preg question

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Try

Code: Select all

<?php
$result = preg_replace("/\[url=(.+)\](.+)\[\/url\]/Uim","<a href="\\1">\\2</a>",$pieces[2]);
?>
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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 ; 
} 
?>
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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; 
} 
?>
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Trying to not cross post.
The question is also being looked at here
http://www.devnetwork.net/forums/viewtopic.php?t=14828
Post Reply