Jump to content

preg_replace


drisate

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.