Re: REGEX help
| From: | Monte Ohrt | Date: | Sat, 21 Oct 2000 16:02:50 +0000 |
| Subject: | Re: REGEX help | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-21565@lists.php.net to get a copy of this message | ||
Here is one idea. Note this code is untested, but you get the idea:
// First, get out all the quoted strings from the text.
// $text contains the original text with the quotes.
// here we match double-quote, followed by anything not double-quote,
followed by double-quote
// (you may need to test this regex against your text and adjust it)
preg_match_all('/"[^"+]"/',$text,$matches);
// put the array of matches into $quoted
$quoted = $matches[0];
// replace all the spaces with underscores, put in $underscored
$underscored = preg_replace("/ /","_" $quoted);
// now replace the quoted strings in the original text
for($loop=0; $loop<count($quoted); $loop++) {
$text = str_replace($quoted[$loop],$underscored[$loop],$text);
}
dbarber wrote:
>
> Hello,
>
> I am trying to replace any spaces in a quoted string without
> replacing any spaces which exist outside of the quotes. IE change this
>
> this is a test "hello I am trying to make this work" to
> this is a test "hello_I_am_trying_to_make_this_work"
>
> thanks in advance,
>
> Dan
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
Monte Ohrt <monte@ispi.net>
http://www.ispi.net/