On 6/15/05, Tom Rogers [EMAIL PROTECTED] wrote:
Hi,
Wednesday, June 15, 2005, 4:42:24 AM, you wrote:
DC On 6/14/05, Sebastian Mendel [EMAIL PROTECTED] wrote:
Dotan Cohen wrote:
Hi gurus. I'm having fun replacing the text within [ and ] to XML
format on 4000+ files on a windows XP machine. I installed php (and an
apache server) on the machine because it is the only language the I am
remotely familiar with.
I want and text that is with [ and ] to be enclosed with note and
/note. Going through the archives I found this nice regex (I changed
it a bit- I hope that I didn't break anything important) that fits
into str_replace:
str_replace(/[([a-zA-Z]+?)]/, note.$what
was_inside_the_parethesis./note, $string);
str_replace doesnt support regular expressions
http://www.php.net/str_replace
what you need is preg_replace()
http://www.php.net/preg_replace
But how on sweet mother Earth do I get the $what
was_inside_the_parethesis variable to stick into the note tags? I
tried functions that would remove the [ and ], then do
$new_str=note.$what was_inside_the_parethesis./note;
and then replace the whole [$what was_inside_the_parethesis] with
$new_str. I did get that far.
preg_replace( '/\[([^\]]+)\]/', 'note\1/note', $string);
Now the catch! If $what_was_inside_the_parethesis contains the word
'algebra' then I want nothing returned- in other words [$what
was_inside_the_parethesis] should be cut out and nothing put in it's
place. I added an if function in there that checked the presence of
the word algebra, but it ALWAYS returned blank.
preg_replace( '/\[(algebra|([^\]]+))\]/', 'note\2/note', $string);
just to mention, you need only the second example, the first ist just to
explain the step toward the final ocde.
Sebastian Mendel
www.sebastianmendel.de
www.sf.net/projects/phpdatetime | www.sf.net/projects/phptimesheet
DC Thank you very much Sebatian! I can't believe that you got that out of
DC one line of code! I often feel that i have a lot to learn, but now I
DC see that I also have a lot to improve...
DC The code works great for [] tags that do not contain the word algebra.
DC If the word algebra is alone between the tags then it returns
DC note/note, which I can easily remove with one more line of code.
DC But if the word algebra appears with another word, then it is
DC converted to a note just as if it did not contain the word.
DC I tried adding asterisks and plus-signs at stratigec places within the
DC regex, but I am not succeeding in getting them to do what I want. I
DC suppose that I would be overstaying my welcome if I asked just where
DC to put them?
DC Dotan
DC http://lyricslist.com/lyrics/artist_albums/402/pink_floyd.php
DC Pink Floyd Lyrics
DC --
DC PHP General Mailing List (http://www.php.net/)
DC To unsubscribe, visit: http://www.php.net/unsub.php
You can use preg_replace_callback to give you a bit more control, so
using Sebastions first expression you could do
function check_string($matches){
if(preg_match('/algebra/i',$matches[0])) return '';
return 'note'.$matches[0].'/note';
}
preg_replaec_callback('/\[([^\]]+)\]/','check_string',$string);
--
regards,
Tom
Thanks, I did not know about the preg_replace_callback function. I'll
start playing with that now. Something new every day!
Dotan
http://lyricslist.com/lyrics/artist_albums/108/carey_mariah.php
Mariah Carey Lyrics
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php