Jump to content

[SOLVED] This regular expression does not parse anything past an Ampersand (&)


Goldeneye

Recommended Posts

Basically: I'm trying to create a regular expression where you input: int://board=1&topic=2 and it outputs Topic 2

Problem: Nothing is being parsed past the ampersand (&); it'll output Board 1&topic=2

The expression:

<?php
$str = 'int://board=1&topic=2'
preg_replace(
'|int://board=([\d]+)&topic=([\d]+)|i',
'<a href="http://foo.bar/forums/topic.php?board=$1&topic=$2" target="_blank">Topic $2</a>',
$str
);
?>

 

I was thinking it's because I'm missing a modifier, but I'm not sure which one (if any) to use.

 

A preemptive thanks.

It works once you add the missing semicolon for $str's defintion and echo out the data, of course.

 

<pre>
<?php
$str = 'int://board=1&topic=2';
echo preg_replace(
'|int://board=([\d]+)&topic=([\d]+)|i',
'<a href="http://foo.bar/forums/topic.php?board=$1&topic=$2" target="_blank">Topic $2</a>',
$str
);
?>
</pre>

Well that's not quite what I'm looking for. That's just a mock-up code to keep things simple though.

 

Here's how I have set up in my script

 

<?php
$text = 'int://board=1&topic=2';

function processText($str){
$str = preg_replace(
	'|int://board=([\d]+)&topic=([\d]+)|i',
	'<a href="http://foo.bar/forums/topic.php?board=$1&topic=$2" target="_blank">Topic $2</a>', 
	$str)
);
return $str;
}

echo processText($text);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.