Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-06-2012, 10:15 AM   PM User | #1
instaunt
New Coder

 
Join Date: Apr 2010
Posts: 92
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
Any way to shorten this preg_match?

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
instaunt is offline   Reply With Quote
Old 12-06-2012, 07:01 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Not tested but something along the lines of the following should work:

Code:
$insert = "how are you today? nice to see you again. here is your login ";
$wwwcheck = $nameinput;
$tpl = preg_replace("/(name[1-4])/i", $insert . '<a href="/folder/$1/">$1</a> ',$nameinput);
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-06-2012, 07:21 PM   PM User | #3
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
I don't know what the $tpl variable contains but I think that the following code will help you.
PHP Code:
if (preg_match('/^name\d+$/i'$nameinput))
        
$tpl preg_replace('/\b' $nameinput '\b/i'$insert '<a href="/folder/' $nameinput '/">' $nameinput '</a> '$tpl); 
gvre is offline   Reply With Quote
Old 12-06-2012, 08:21 PM   PM User | #4
instaunt
New Coder

 
Join Date: Apr 2010
Posts: 92
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
$temp_page = preg_replace("/name1|name2|name3|name4/i", '<a href="/folder/$1/">$1</a> ',$temp_page);

did not work. even:

$temp_page = preg_replace("/name1|name2|name3|name4/i", $1,$temp_page);

did not work.

if (preg_match('/^name\d+$/i', $nameinput))
$tpl = preg_replace('/\b' . $nameinput . '\b/i', $insert . '<a href="/folder/' . $nameinput . '/">' . $nameinput . '</a> ', $tpl);


did nothing either.

Here is a simplified/explanatory version of what I want to do:

$temp_page = preg_replace("/manchester|leeds|bradford|london/i", '<a href="/folder/$1/">$1</a> ',$temp_page);

Which results in every instance of "Manchester" or "Leeds" in the $temp_page section becoming:

<a href="/folder/manchester/">Manchester</a>
<a href="/folder/leeds/">Leeds</a>

Last edited by instaunt; 12-06-2012 at 08:27 PM..
instaunt is offline   Reply With Quote
Old 12-06-2012, 09:02 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by instaunt View Post
$temp_page = preg_replace("/name1|name2|name3|name4/i", '<a href="/folder/$1/">$1</a> ',$temp_page);

did not work. even:

$temp_page = preg_replace("/name1|name2|name3|name4/i", $1,$temp_page);

did not work.
Neither of those worked because you didn't define $1 by wrapping the part of the regular expression you want to substitute inside of () like this:

preg_replace("/(name1|name2|name3|name4)/i", '<a href="/folder/$1/">$1</a> ',$temp_page)
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-06-2012, 10:04 PM   PM User | #6
instaunt
New Coder

 
Join Date: Apr 2010
Posts: 92
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
Neither of those worked because you didn't define $1 by wrapping the part of the regular expression you want to substitute inside of () like this:

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

Any way to get the URl $1 to be lowercase? I've tried sticking strtolower in there in various forms, but it just breaks the code. Is the $1 usage causing a problem doing anything else with it?

Thanks again
Mike
instaunt is offline   Reply With Quote
Old 12-06-2012, 10:26 PM   PM User | #7
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
You could convert every name to lowercase before the preg_replace. One other option is to use the preg_replace_callback function (check the following code).

PHP Code:
// needs php >= 5.3.0
$tpl preg_replace_callback('/\b(name1|name2|name3|name4)\b/i', function($m) use ($insert) { return $insert '<a href="/folder/' strtolower($m[0]) . '/">' $m[0] . '</a> '; }, $tpl); 
gvre is offline   Reply With Quote
Old 12-07-2012, 11:58 AM   PM User | #8
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
btw, str_replace() also accepts arrays as input.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:12 PM.


Advertisement
Log in to turn off these ads.