drisate Posted September 3, 2008 Share Posted September 3, 2008 Hey guys i have a tag that looks like this [CAL=02/2008] i tried using $content = preg_replace('/\[CAL(=([0-9]+/[0-9]))?\]/ie', "getJobs($2, 'CAL')", $content); but it gives me this error Warning: preg_replace() [function.preg-replace]: Unknown modifier '[' in /home/direct/public_html/administrator/components/com_acajoom/classes/class.jmail.php on line 160 Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/ Share on other sites More sharing options...
rarebit Posted September 3, 2008 Share Posted September 3, 2008 maybe you need to escape the central /, e.g. \/ ? maybe it's treating that as end of statement Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633126 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 Yeah, you need to escape the \ in the middle: $content = preg_replace('/\[CAL(=([0-9]+\/[0-9]))?\]/ie', "getJobs($2, 'CAL')", $content); You may want to write another function other than getJobs() if you're going to be doing more things. It's better than passing all those params in and stuff. >_< Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633129 Share on other sites More sharing options...
drisate Posted September 3, 2008 Author Share Posted September 3, 2008 hehe this is what i was planning to do if (!function_exists('getJobs')) { function getJobs($limit=NULL, $type) { if ($type=="JOB"){$table="jos_jl_jobposting"; $col="title";} if ($type=="SHOP"){$table="jos_marketplace_ads"; $col="ad_headline";} if ($type=="CAL"){$table="jos_extcal_events"; $col="title"; $limit="10";} $query = "SELECT * FROM $table"; if ($limit != null) { $query .= " LIMIT $limit"; } $res = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($res)) { $jobs[] = $row[$col]; } return implode('<br />', $jobs); } } $content = preg_replace('/\[JOB(=([0-9]+))?\]/ie', "getJobs($2, 'JOB')", $content); $content = preg_replace('/\[sHOP(=([0-9]+))?\]/ie', "getJobs($2, 'SHOP')", $content); $content = preg_replace('/\[CAL(=([0-9]+\/[0-9]))?\]/ie', "getJobs($2, 'CAL')", $content); The first 2 works great but [CAL=2008/10] is not recognized. the reason i added $limit="10"; to the CAL if type is because i did not create the SQL string to retrieve the data by date. Anyway, looks like $content = preg_replace('/\[CAL(=([0-9]+\/[0-9]))?\]/ie', "getJobs($2, 'CAL')", $content); is not recognizing [CAL=2008/10]. Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633148 Share on other sites More sharing options...
rarebit Posted September 3, 2008 Share Posted September 3, 2008 do you understand how the () brackets create numbered groups? It's ages since I need to use regex so I can't rem how they'll effect this... Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633153 Share on other sites More sharing options...
drisate Posted September 3, 2008 Author Share Posted September 3, 2008 Well i just tryed this $content = preg_replace('/\[CAL(=([0-9]+)\/([0-9]))?\]/ie', "getJobs($2, 'CAL')", $content); And it's not working either ... [CAL=2008/10] stays unreplaced The reason why i tryed it in the same () is because 2008/10 should be 1 value But i am not an expert on the question so i might be wrung ... Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633157 Share on other sites More sharing options...
rarebit Posted September 3, 2008 Share Posted September 3, 2008 $content = "[CAL=2008/10]"; $content = preg_replace('/(\[CAL\=([0-9]*)\/([0-9]*)\])/', 'XXX', $content); print "content: ".$content."<br><br>"; or something like! Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633170 Share on other sites More sharing options...
drisate Posted September 3, 2008 Author Share Posted September 3, 2008 OMG ! thx rarebit i added ie to your string and it worked thank you so much bro! Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633176 Share on other sites More sharing options...
nrg_alpha Posted September 3, 2008 Share Posted September 3, 2008 If I am not mistaken, I think you need to use preg_replace_callback if you wish to create / call a function from within the replace aspect of preg. Granted, I have been known to be wrong (on more occasions than I care to admit). Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633257 Share on other sites More sharing options...
DarkWater Posted September 4, 2008 Share Posted September 4, 2008 If I am not mistaken, I think you need to use preg_replace_callback if you wish to create / call a function from within the replace aspect of preg. Granted, I have been known to be wrong (on more occasions than I care to admit). Nah, you can use the e modifier to make the replace actually an eval'd portion of code. I like it better for times where you may need to add things around the sides of the function output or do formatting or something. Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633282 Share on other sites More sharing options...
nrg_alpha Posted September 4, 2008 Share Posted September 4, 2008 If I am not mistaken, I think you need to use preg_replace_callback if you wish to create / call a function from within the replace aspect of preg. Granted, I have been known to be wrong (on more occasions than I care to admit). Nah, you can use the e modifier to make the replace actually an eval'd portion of code. I like it better for times where you may need to add things around the sides of the function output or do formatting or something. Ah, just tried it. Nice to know Will definitely come in handy to be sure. Quote Link to comment https://forums.phpfreaks.com/topic/122620-preg_replace/#findComment-633286 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.