-1

I try replace characters from string and put others , for this use preg_replace_callback function , the problem is this function change order of original text and show differents results

For example if the string it´s :

Hello everybody [1-2-3] this it´s one test [4-5-6-7-8] --- > ORIGINAL TEXT

The script search the [ ] and separate this content with [ ] from the other text , but show me as i put here :

1-2-3 4-5-6-7-8 Hello Everybody this it´s one test --- > BAD RESULT

When the right order it´s the first without [ ]

Hello everybody 1-2-3 this it´s one test 4-5-6-7-8

The script i create :

<?php

$text = " This is a test [gal~ruta~100~100] This is other test [gal~ruta2~100~100]";

function gal($matches)
{

global $text;

$exp=explode("~",$matches[1]);

$end=str_replace($matches[1],$a,$text);

if ($exp[0]=="gal")
{
$a="".$exp[1]."".$exp[2]."".$exp[3]."";
echo $a;
}


}


echo preg_replace_callback(
"/\[(.*?)\]/",
"gal",
$text);

?>

Thank´s everybody for the help

Robert
  • 315
  • 2
  • 5
  • 9

1 Answers1

1

You need to return something from your callback, which will be substituted in the original string. You most certainly don't want to echo, which will send the values straight to the output, in order of execution.

However, it sounds like you don't even need a callback function to remove the brackets around your numbers.

$str = preg_replace("/\[(.*?)\]/", "$1", $str);

CodePad.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Yes right in the original string i use shortcodes and need replace this shortcodes for function which read gallery and show it , but i don´t get works and show the original string and replace these shortcodes for the function of gallery , Thank´s !!! – Robert Nov 28 '12 at 04:25
  • Yes right but these tags work as shortcode and need replace by function and in each case show gallery , and i can´t do this , i try many times and i can´t do works or if worls the order of text change ..... iif you need see all code i can put , thank´s – Robert Nov 28 '12 at 04:35