homepage Welcome to WebmasterWorld Guest from 54.196.168.78
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Any way to shorten this preg match?
internetheaven




msg:4525627
 7:55 pm on Dec 6, 2012 (gmt 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

 

g1smd




msg:4525641
 8:23 pm on Dec 6, 2012 (gmt 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.

internetheaven




msg:4525644
 8:28 pm on Dec 6, 2012 (gmt 0)

Ah! I wondered what had happened to the original post!

internetheaven




msg:4525694
 10:05 pm on Dec 6, 2012 (gmt 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

lucy24




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

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 ::

lucy24




msg:4525736
 1:05 am on Dec 7, 2012 (gmt 0)

:: 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 :(

brotherhood of LAN




msg:4525737
 1:09 am on Dec 7, 2012 (gmt 0)

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'.

internetheaven




msg:4525904
 2:12 pm on Dec 7, 2012 (gmt 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

g1smd




msg:4525924
 3:23 pm on Dec 7, 2012 (gmt 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.

lucy24




msg:4525985
 7:51 pm on Dec 7, 2012 (gmt 0)

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.

lucy24




msg:4526036
 3:10 am on Dec 8, 2012 (gmt 0)

:: 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.")

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved