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
https://forums.phpfreaks.com/topic/227843-switch-inside-preg_replace/
Share on other sites

  Quote

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

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;
?>

Archived

This topic is now archived and is closed to further replies.

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