Re: Re: Converting [and] to XML format- help/advise requested

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 6/16/05, Sebastian Mendel <lists@xxxxxxxxxxxxxxxxxx> wrote:
> Dotan Cohen wrote:
> > On 6/15/05, Sebastian Mendel <lists@xxxxxxxxxxxxxxxxxx> 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.
> >>> The code works great for [] tags that do not contain the word algebra.
> >>> If the word algebra is alone between the tags then it returns
> >>> <note></note>, which I can easily remove with one more line of code.
> >>> But if the word algebra appears with another word, then it is
> >>> converted to a note just as if it did not contain the word.
> >>
> >> $search = array(
> >>     '/\[[^\]*algebra[^\]*\]/',
> >>     '/\[([^\]]+)\]/',
> >> );
> >>
> >> $replace = array(
> >>     '',
> >>     '<note>\1</note>',
> >> );
> >>
> >> preg_replace( $search, $replace, $string);
> >
> > Thank you Sebastian. Know that I not only appreciate your assistance,
> > but that I also make a genuine effort to learn from your example. I
> > think that it's about time that I learned to brew regexes. On my to
> > google now...
> 
> there are just two things you need to learn regular expressions:
> 
> the PHP-manual:
> 
> http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
> http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
> 
> http://www.php.net/manual/en/ref.pcre.php
> http://www.php.net/manual/en/ref.regex.php
> 
> and the regex coach:
> 
> http://www.weitz.de/regex-coach/
> 
> 
> --
> Sebastian Mendel
> 
> www.sebastianmendel.de
> www.sf.net/projects/phpdatetime | www.sf.net/projects/phptimesheet
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 

I just got to playing around with the code that you sent. I had a lot
of fun these past few hours, but I feel that I learned a lot. The code
that you sent was throwing errors:
Warning:  preg_replace(): Compilation failed: missing terminating ]
for character class at offset 21 in

So I decided that the time had come for me to learn regexes. It took
me a little over two hours, but I built the regex myself and came up
with this:
$string=preg_replace( "/\[[^\]]*algebra[^\[]*\]/i", '', $string);

Which is, I think, the exact regex that you tried to send me. Yours:
preg_replace( '/\[[^\]*algebra[^\]*\]/', '', $string);

was missing the ] and [ inside the 'ignore these' brackets. I also
added the i flag for case-insensitivity and, of course, the $string=
to the beginning.

I'm not complaining, mind you. I very much appreciate what you have
done: you have taught me. I can now look at your regex, understand it,
and find the mistake! I could not do that a few hours ago. I just
wanted to show you that there is an error so that if you recycle this
code somewhere you will be aware. And I wanted to say thank you.

Dotan
http://lyricslist.com/lyrics/artist_albums/345/metallica.php
Metallica Lyrics

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux