Jump to content

switch() inside preg_replace


ejaboneta

Recommended Posts

I am trying to add some quick tagging features to my site where you put in a code like "[img:1]" and my function will translate that into an image with an id of 1. I though I could put the whole page in a variable and replace anywhere that matched the pattern. When I try to use a switch() inside the function that replaces the preg_replace matches, the variables I expect to be there, aren't there.  It isn't working because the variable "IMG" and "1" show up as "$1" and "$2" to the code. Is there another way to do this?

 

 

 

 

  
function gCodes($code,$var) {
   switch($code) {
    case "IMG":
      $img = getImage($var);
      $return = "<a href=\"$img[location]\"><img src=\"$img[location]\" alt=\"$img[title]\"/></a>";
      break;
    default:
      $return = "$code=>$var<br>Not Working<br>";
  break;
  }          
return $return;
}
  $text = "[img:1]";
  $content = preg_replace("/\[([A-Z]{3})[a-zA-Z0-9]+)\]/",gCodes("$1","$2"),$text);
   echo $content;

 

The above example prints out:

IMG=>1
Not Working

Link to comment
Share on other sites

I am trying to add some quick tagging features to my site where you put in a code like "[img:1]" and my function will translate that into an image with an id of 1. I though I could put the whole page in a variable and replace anywhere that matched the pattern. When I try to use a switch() inside the function that replaces the preg_replace matches, the variables I expect to be there, aren't there.  It isn't working because the variable "IMG" and "1" show up as "$1" and "$2" to the code. Is there another way to do this?

 

 

  
function gCodes($code,$var) {
   switch($code) {
    case "IMG":
      $img = getImage($var);
      $return = "<a href=\"$img[location]\"><img src=\"$img[location]\" alt=\"$img[title]\"/></a>";
      break;
    default:
      $return = "$code=>$var<br>Not Working<br>";
  break;
  }          
return $return;
}
  $text = "[img:1]";
  $content = preg_replace("/\[([A-Z]{3})[a-zA-Z0-9]+)\]/",gCodes("$1","$2"),$text);
   echo $content;

 

The above example prints out:

IMG=>1
Not Working

 

Even though $code prints out as "IMG", the switch doesn't see it as such.

 

Oh and sorry, I didn't mean to reply to my post, I meant to modify it and I don't know how to delete it(if I can).

Link to comment
Share on other sites

try

<?php
function gCodes($array) {
    $code = $array[1];
    $var = $array[2];
   switch($code) {
    case "IMG":
      $img = getImage($var);
      $return = "<a href=\"$img[location]\"><img src=\"$img[location]\" alt=\"$img[title]\"/></a>";
      break;
    default:
      $return = "$code=>$var<br>Not Working<br>";
  break;
  }          
return $return;
}
  $text = "[img:1]";
  $content = preg_replace_CALLBACK("/\[([A-Z]{3})[a-zA-Z0-9]+)\]/",'gCodes',$text);
   echo $content;
?>

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.