Welcome to WebmasterWorld Guest from 54.145.65.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Any way to shorten this preg match?

     
7:55 pm on Dec 6, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 25, 2003
posts:2527
votes: 0


I currently have a code like this:

$insert = "how are you today? nice to see you again. here is your login ";
$wwwcheck = $nameinput;
if(preg_match("/name1|name2|name3|name4/i", $nameinput)) {
$tpl = str_replace('name1 ', $insert . '<a href="/folder/name1/">name1</a> ', $tpl);
$tpl = str_replace('name2 ', $insert . '<a href="/folder/name2/">name2</a> ', $tpl);
$tpl = str_replace('name3 ', $insert . '<a href="/folder/name3/">name3</a> ', $tpl);
$tpl = str_replace('name4 ', $insert . '<a href="/folder/name4/">name4</a> ', $tpl);
}

which has 60 lines and is only going to keep getting bigger. Is there anyway to shrink the principal down to a single line?

e.g.

$insert = "how are you today? nice to see you again. here is your login ";
$wwwcheck = $nameinput;
if(preg_match("/name1|name2|name3|name4/i", $nameinput)) {
$tpl = str_replace('$name ', $insert . '<a href="/folder/$name/">$name</a> ', $tpl);
}

Really struggling.

Thanks
Mike
8:23 pm on Dec 6, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


I posted a long answer to this, this morning, but the server glitch ate it. I'll try to
1. remember what I said,
2. repost it again
later on.

Shame you didn't have email notification switched on, as you could have pasted it back in here.
8:28 pm on Dec 6, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 25, 2003
posts:2527
votes: 0


Ah! I wondered what had happened to the original post!
10:05 pm on Dec 6, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 25, 2003
posts:2527
votes: 0


preg_replace("/(name1|name2|name3|name4)/i", '<a href="/folder/$1/">$1</a> ',$temp_page)

Is what I've been given elsewhere and it seems to work. I just need to get the URl $1 to be lower case ... not going well.

Mike
11:43 pm on Dec 6, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


I just need to get the URl $1 to be lower case ... not going well.

You can't use the same raw $1 for both. The visible part yes. The non-visible part needs

:: shuffling papers ::

strtolower. Or mb_strtolower if you've got names with letters outside the vanilla ASCII a-z range. At a guess,

'<a href="/folder/'.strtolower($1).'/">$1</a>'

but don't quote me.

:: off for some trial & error to see how far wrong I am ::
1:05 am on Dec 7, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


:: returning after prolonged battle with php dot net ::

OK, so I only had seven things wrong. Got this far:

$first =
preg_replace_callback('/(name1|name2|name3|name4)/',
create_function('$match','return strtolower($match[0]);'),
$temp_page);

$second =
preg_replace('/(name1|name2|name3|name4)/','<a href="/folder/'.$first.'/">$1</a>',$temp_page);

which can be inelegantly packed into

$second =
preg_replace('/(name1|name2|name3|name4)/','<a href="/folder/'.(preg_replace_callback('/(name1|name2|name3|name4)/',
create_function('$match','return strtolower($match[0]);'),
$temp_page)).'/">$1</a>',$temp_page);

I experimented with values of my own, replacing <> with [] and throwing in an echo to see the result. So if anything looks completely unfamiliar, it means I missed something in the final cut-and-paste.

Can you take it from there? I'm quite certain I overlooked something, since I end up saying everything twice :(
1:09 am on Dec 7, 2012 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4843
votes: 2


RE: PHP in 'replace'

There is a flag you can use in the regex that tells PHP there's PHP code to evaluate in the replace string... i believe it's 'e'.
2:12 pm on Dec 7, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 25, 2003
posts:2527
votes: 0


Thank you. Got it working ... can't use it!

I failed to plan ahead enough to realise that some of the names are already inside <a href> tags under other uses. So I ended up with:

On Thursday <a href='/folder2/out.php'>we found <a href='/folder/name2/'>name2</a> stuff here</a> and it looks pretty good.

I can't imagine that there is any way for preg_replace/match to ignore any string already within HTML tags e.g. href, img etc.

Again, thanks a lot. And this will be useful on other sites where coding has bloated.

Mike
3:23 pm on Dec 7, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Yes, there is. You test for it and then do something or not, OR you can arrange the RegEx pattern so that you "capture" only the name part.
7:51 pm on Dec 7, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


You can test for the text before the name-- it would be something other than "> -- and/or you can use a non-capturing lookahead or lookbehind. Just be careful constructing the lookahead. Otherwise the RegEx is liable to omit part of the name in order to get a match.
3:10 am on Dec 8, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


:: darn those time limits anyway ::

php dot net says the /e flag is deprecated as of php 5.5 (which I haven't even got), but this is the slightly shorter version:

preg_replace_callback
('/(name1|name2|name3|name4)/',
function($demo)
{ return '<a href="/folder/' . strtolower($demo[0]) . '/">' . $demo[0] . '</a>'; }
,
$temp_page);


"$demo" is whatever name you want to make up for your RegEx. Instead of $1 you say $demo[1] -- or in this case $demo[0]. Both mean exactly the same thing, because you are capturing the entire expression. Otherwise [0] means the full RegEx and then the numbers mean your consecutive captures. (This bit works exactly like .exec in javascript.)

Watch out for those evil punctuation marks. One of the very very few things I know about php is that it goes into a coma if you leave out a semicolon. otoh I know a fair amount about constructing regular expressions ;) ("Find all occurrences of Greek text in this 400-page book.")