Is there something wrong/missing with this?

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
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Is there something wrong/missing with this?

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Is there something wrong/missing with this?

Post 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 &nbsp; not by a space. The expression won't match.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Is there something wrong/missing with this?

Post 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 :?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Is there something wrong/missing with this?

Post by requinix »

To address that issue only, try

Code: Select all

/\b[i]word[/i]\S*/
for the pattern, using $0 in the replacement string (not $1).
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Is there something wrong/missing with this?

Post 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);
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Is there something wrong/missing with this?

Post 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.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Is there something wrong/missing with this?

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Is there something wrong/missing with this?

Post by requinix »

Code: Select all

"/\$input1lc\S*/"
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);
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Is there something wrong/missing with this?

Post 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.
Post Reply