Jump to content

[SOLVED] stating multiple replacments preg_replace


nadeemshafi9

Recommended Posts

Hi guys

 

How do you replace multiple paterns with a single replacement using regex.

 

here is my code

 

$tn = ereg_replace(" ", "_", $template_name);
                    $tn = ereg_replace(".html", "", $tn);
                    $tn = ereg_replace(".htm", "", $tn);

                    $f = ereg_replace(" ", "_", $filesToAdd_v);
                    $f = ereg_replace(".html", "", $f);
                    $f = ereg_replace(".htm", "", $f);

 

how do i get that into one line for each var tn and f

 

thanks

Link to comment
Share on other sites

nadeem, I would ditch ereg as POSIX is technically depreciated as of PHP 5.3 and will not be included within the core as of 6. Using PCRE is the prefered regex flavor.

 

yes i know that and i am trying my best to use preg but by reflex i use ereg, the thing is that i dont think the app will port into php 6 anyways so whats the point, but yes i do use preg instead now.

Link to comment
Share on other sites

Original

$tn = ereg_replace(" ", "_", $template_name);
$tn = ereg_replace(".html", "", $tn);
$tn = ereg_replace(".htm", "", $tn);

$f = ereg_replace(" ", "_", $filesToAdd_v);
$f = ereg_replace(".html", "", $f);
$f = ereg_replace(".htm", "", $f);

 

One-liner regular expressions

$tn = preg_replace(array("/ /", "/\.html?/"), array("_", ""), $template_name);
$f  = preg_replace(array("/ /", "/\.html?/"), array("_", ""), $filesToAdd_v);

 

String replacements

$tn = str_replace(array(" ", ".html", ".htm"), array("_", "", ""), $template_name);
$f  = str_replace(array(" ", ".html", ".htm"), array("_", "", ""), $filesToAdd_v);

 

One-liner

list($tn, $f) = str_replace(array(" ", ".html", ".htm"), array("_", "", ""), array($template_name, $filesToAdd_v));

 

 

Link to comment
Share on other sites

yes i know that and i am trying my best to use preg but by reflex i use ereg, the thing is that i dont think the app will port into php 6 anyways so whats the point, but yes i do use preg instead now.

 

Well, I think it boils down to habit more than anything else. Sticking with ereg relfex habits will come back to haunt you sooner or later (as sooner or later [most likely the latter], hosting providers will ditch PHP 5.x in favor of 6, once 5.x is old enough and falls out of favor [much like what's happening with PHP 4]). By future proofing your code now (getting into PCRE habits), you won't have to worry about the day your code breaks in the event your hosting provider switches to 6 and doesn't happen to include POSIX's extention.

 

Or rephrased another way, sticking with PCRE, you can't go wrong.. but sticking with POSIX, something can go wrong down the line. Ultimately, it's your call.. just trying to encourage going the safer route is all.

Link to comment
Share on other sites

yes i know that and i am trying my best to use preg but by reflex i use ereg, the thing is that i dont think the app will port into php 6 anyways so whats the point, but yes i do use preg instead now.

 

Well, I think it boils down to habit more than anything else. Sticking with ereg relfex habits will come back to haunt you sooner or later (as sooner or later [most likely the latter], hosting providers will ditch PHP 5.x in favor of 6, once 5.x is old enough and falls out of favor [much like what's happening with PHP 4]). By future proofing your code now (getting into PCRE habits), you won't have to worry about the day your code breaks in the event your hosting provider switches to 6 and doesn't happen to include POSIX's extention.

 

Or rephrased another way, sticking with PCRE, you can't go wrong.. but sticking with POSIX, something can go wrong down the line. Ultimately, it's your call.. just trying to encourage going the safer route is all.

 

most of the systems i developed are large scale so they are sitting on in house custom built servers made just for them and they have there personal interface to the internet eg there own lines, they will be kept as legacy systems and code.

 

i completely understand what your saying thogh, good practice is good practice.

Link to comment
Share on other sites

most of the systems i developed are large scale so they are sitting on in house custom built servers made just for them and they have there personal interface to the internet eg there own lines, they will be kept as legacy systems and code.

 

i completely understand what your saying thogh, good practice is good practice.

 

As reassuring is it might look to you, think for a moment about future developers who will curse you like hell if they'll have to maintain your code ;) Just like people who reqrite whole scripts basing on register_globals do now :)

 

Think about our children!

Link to comment
Share on other sites

Original

$tn = ereg_replace(" ", "_", $template_name);
$tn = ereg_replace(".html", "", $tn);
$tn = ereg_replace(".htm", "", $tn);

$f = ereg_replace(" ", "_", $filesToAdd_v);
$f = ereg_replace(".html", "", $f);
$f = ereg_replace(".htm", "", $f);

 

One-liner regular expressions

$tn = preg_replace(array("/ /", "/\.html?/"), array("_", ""), $template_name);
$f  = preg_replace(array("/ /", "/\.html?/"), array("_", ""), $filesToAdd_v);

 

String replacements

$tn = str_replace(array(" ", ".html", ".htm"), array("_", "", ""), $template_name);
$f  = str_replace(array(" ", ".html", ".htm"), array("_", "", ""), $filesToAdd_v);

 

One-liner

list($tn, $f) = str_replace(array(" ", ".html", ".htm"), array("_", "", ""), array($template_name, $filesToAdd_v));

 

 

 

dude thanks thats realy good

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.