Jump to content

regular expression


arunpatal

Recommended Posts

Hello everyone,

 

example string

<?php

$string = "";
$string  .= "<textarea id='convert'> 'AA' </textarea>";
$string  .= "<textarea id='convert'> 'BB' </textarea>";
$string  .= "<textarea id='not_convert'> 'AA' </textarea>";
$string  .= "<textarea id='not_convert'> 'DD' </textarea>";

?>

I want to change 'AA' to 'ZZ' which is inside textarea with the id convert only.

 

How is it possible with the help of regular expression?

Link to comment
https://forums.phpfreaks.com/topic/289157-regular-expression/
Share on other sites

If the textareas always look the same, you could use str_replace():

http://www.php.net//manual/en/function.str-replace.php

 

str_replace() will not solve the problem...

 

i can change textarea a bit if its requeired but lets say if string is like this and i want to change all AA to ZZ

<?php

$string = "";
$string  .= '<textarea id="convert"> "AA"  KAA="AA"  $AA</textarea>';
$string  .= "<textarea id='convert'> 'BB' </textarea>";
$string  .= "<textarea id='not_convert'> 'AA' </textarea>";
$string  .= "<textarea id='not_convert'> 'DD' </textarea>";

?>
Link to comment
https://forums.phpfreaks.com/topic/289157-regular-expression/#findComment-1482657
Share on other sites

 

Could try this if your using PHP 5.3.0 or newer

$string = preg_replace_callback('/(<textarea id="convert">)(.*?)(<\/textarea>)/is'

                                    ,function($m) {
                                        array_shift($m);
                                        $m[1] = str_replace('AA', 'ZZ', $m[1]);
                                        return implode($m);
                                    }

                                ,$string);

echo $string;

 

Sorry but how do i  implement this code with the above code i wrote?

Link to comment
https://forums.phpfreaks.com/topic/289157-regular-expression/#findComment-1482659
Share on other sites

After you have defined the  $string variable.

$string = "";
$string  .= "<textarea id='convert'> 'AA' </textarea>";
$string  .= "<textarea id='convert'> 'BB' </textarea>";
$string  .= "<textarea id='not_convert'> 'CC' </textarea>";
$string  .= "<textarea id='not_convert'> 'DD' </textarea>";


$string = preg_replace_callback('/(<textarea id="convert">)(.*?)(<\/textarea>)/is'

                                    ,function($m) {
                                        array_shift($m);
                                        $m[1] = str_replace('AA', 'ZZ', $m[1]);
                                        return implode($m);
                                    }

                                ,$string);

echo $string;

I tried this way but its not working....

Link to comment
https://forums.phpfreaks.com/topic/289157-regular-expression/#findComment-1482664
Share on other sites

Because you are using single quotes for denoting the textarea id. Change your single quotes to double quotes instead

$string = "";
$string  .= "<textarea id=\"convert\"> 'AA' </textarea>";
$string  .= "<textarea id=\"convert\"> 'BB' </textarea>";
$string  .= "<textarea id=\"not_convert\"> 'CC' </textarea>";
$string  .= "<textarea id=\"not_convert\"> 'DD' </textarea>";
Link to comment
https://forums.phpfreaks.com/topic/289157-regular-expression/#findComment-1482668
Share on other sites

 

Because you are using single quotes for denoting the textarea id. Change your single quotes to double quotes instead

$string = "";
$string  .= "<textarea id=\"convert\"> 'AA' </textarea>";
$string  .= "<textarea id=\"convert\"> 'BB' </textarea>";
$string  .= "<textarea id=\"not_convert\"> 'CC' </textarea>";
$string  .= "<textarea id=\"not_convert\"> 'DD' </textarea>";

Thanks..  its working

Link to comment
https://forums.phpfreaks.com/topic/289157-regular-expression/#findComment-1482670
Share on other sites

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.