Jump to content

regular expression


arunpatal
Go to solution Solved by Ch0cu3r,

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

?>
Edited by arunpatal
Link to comment
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
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
Share on other sites

  • Solution

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