Page 1 of 1
Is there something wrong/missing with this?
Posted: Fri Oct 17, 2008 9:46 am
by someguyhere
Code: Select all
srand(time());
shuffle($input4);
$newArr = array_slice($input4, 0, 17);
$par1=implode(" ", $newArr);
$input1lc = strtolower($input1[$rand_key1]);
$par1=preg_replace("/(\S+?$input1lc\S+?)/", "<a href=\"$url1\" />$1[/]", $par1);
It should create a linked version of a word within a paragraph, but I get no results.
Also, is there a way to make this only occur once in a paragraph rather than every occurance of the word?
Re: Is there something wrong/missing with this?
Posted: Fri Oct 17, 2008 2:57 pm
by requinix
Well you don't print anything... don't see where you define $input4, or $input1, or $rand_key1, or $url1...
As for your other issue, preg_replace has an (optional) limit parameter.
And the preg_replace won't affect anything either: each part is surrounded by a not by a space. The expression won't match.
Re: Is there something wrong/missing with this?
Posted: Fri Oct 17, 2008 9:35 pm
by someguyhere
Ah...I suppose a little addtional info would be useful.
Line 6 is where the problem is - I originally had a different line of code to do that which worked about 99% correctly. It is supposed to replace occurances of a word in the paragraph with a hyperlinked version of the word, but the problem is that if $input1 was "boat" and the word "boating" occured in the paragraph, only "boat" would be linked, not the "ing" - In this case I would want it to link the entire word.
So how can I have it make a hyperlinked version of the word, but not close the link until it finds the end of the entire word.
Hoepfully that is a better explaination

Re: Is there something wrong/missing with this?
Posted: Sat Oct 18, 2008 1:28 am
by requinix
To address that issue only, try
for the pattern, using $0 in the replacement string (not $1).
Re: Is there something wrong/missing with this?
Posted: Sat Oct 18, 2008 8:26 pm
by someguyhere
I've been working with it for a few hours now but I can't get it to work. Am I missing something obvious?
Code: Select all
$par1=preg_replace("/\$input1\S*/", "<a href=\"$url1\">$0</a>", $par1);
Re: Is there something wrong/missing with this?
Posted: Sat Oct 18, 2008 9:12 pm
by requinix
Depends how it's not working.
Unless you changed something, $input1 is an array. You can't stick it directly into a string and expect PHP to know what you want done with it.
Re: Is there something wrong/missing with this?
Posted: Sun Oct 19, 2008 1:49 pm
by someguyhere
I missed that...changed it to:
Code: Select all
$par1=preg_replace("/\$input1lc\S*/", "<a href=\"$url1\">$0</a>", $par1);
But it is still not hyperlinking the text.
Re: Is there something wrong/missing with this?
Posted: Sun Oct 19, 2008 4:02 pm
by requinix
Why did you escape the $?
Still wondering why you haven't tried
Code: Select all
preg_replace("/\\b$input1lc\\S*/i", "<a href=\"$url1\">\$0</a>", $par1);
Re: Is there something wrong/missing with this?
Posted: Mon Oct 20, 2008 8:41 am
by someguyhere
Still wondering why you haven't tried
Code: Select all
preg_replace("/\\b$input1lc\\S*/i", "<a href=\"$url1\">\$0</a>", $par1);
I just tried that, but still no link.