nadeemshafi9 Posted November 4, 2009 Share Posted November 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/ Share on other sites More sharing options...
Mchl Posted November 4, 2009 Share Posted November 4, 2009 http://www.regular-expressions.info/alternation.html Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/#findComment-950855 Share on other sites More sharing options...
nadeemshafi9 Posted November 4, 2009 Author Share Posted November 4, 2009 http://www.regular-expressions.info/alternation.html thanks Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/#findComment-950863 Share on other sites More sharing options...
nrg_alpha Posted November 4, 2009 Share Posted November 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/#findComment-951088 Share on other sites More sharing options...
nadeemshafi9 Posted November 4, 2009 Author Share Posted November 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/#findComment-951090 Share on other sites More sharing options...
salathe Posted November 4, 2009 Share Posted November 4, 2009 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)); Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/#findComment-951097 Share on other sites More sharing options...
nrg_alpha Posted November 4, 2009 Share Posted November 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/#findComment-951099 Share on other sites More sharing options...
nadeemshafi9 Posted November 4, 2009 Author Share Posted November 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/#findComment-951109 Share on other sites More sharing options...
Mchl Posted November 4, 2009 Share Posted November 4, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/#findComment-951111 Share on other sites More sharing options...
nadeemshafi9 Posted November 4, 2009 Author Share Posted November 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/180253-solved-stating-multiple-replacments-preg_replace/#findComment-951112 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.