![]() |
|
#1
|
|||
|
|||
preg_replace question...Hello -
I've put together a little php script to grab forum posts and parse the bulletin codes into html, however one particular transition seems to be making it screw up. I just wanted to figure out whats going on, cause I seem to be missing something completely with this. Heres a little snippet of the preg_replace portion.... PHP Code:
For the most part this works great, however the URL (and possibly email too) seem to have problems working properly. For the following BB post: Code:
It returns this result: [html] ...about it <A HREF="http://www.mysite.com/forums/showthread.php?s=&threadid=3357]here[/url]. For more information, check out the post [url=http://www.mysite.com/forums/showthread.php?s=&threadid=3598">here</A>. Do your... [/HTML] So what I would like to know is why its skipping the first match for the url regex? Anyone got any possible ideas? Much thanks. -Perc |
|||
|
#2
|
|||
|
|||
|
Oh, and on an odd side note of cleaning that above php up, is there a better way to check for both caps and lowercase in a regex than [U|u][R|r][L|l]?
Thanks. -Perc |
|
#3
|
||||
|
||||
PCRE (Perl Compatible Regular Expressions) Pattern ModifiersHello Perculator,
You need what is known as PCRE Pattern Modifiers in your patterns above. An example: PHP Code:
As for your second problem, this again is a PATTERN MODIFIER issue, the Ungreedy pattern modifier specifically. Again, to show you how you could use these PCRE pattern modifiers more efficiently in your existing PHP script snippet above, refer to the example code below: PHP Code:
For more detailed information on the various available PCRE Pattern Modifiers, please refer to this page: http://www.php.net/manual/en/pcre.pattern.modifiers.php __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#4
|
|||
|
|||
|
Yup, that's exactly it. Preg functions, by default, are supposed to be as greedy as possible, so using the U modifier tells it not to be.
GF |
|
#5
|
|||
|
|||
|
Spectacular. Worked like a charm.
My last question as a follow up, is why were the bold and italic replacements not being greedy? There were several bold and italic sections, but they all converted properly. I think that's what was throwing me off the most....how it was working for some, and not others. Reguardless, thank you very much for the quick and useful reply... -Perc |
|
#6
|
||||
|
||||
|
Quote:
What I mean actually: since you did NOT use the "s" (or PCRE_dotall) pattern modifier in your patterns, multiple [b] tags will work quite well, as LONG as each pair appear in separate lines - but the minute there are multiple PHP Code:
PHP Code:
__________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#7
|
|||
|
|||
|
Very interesting, and very cool. Much thanks JDS...
-Perc |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| question of practice | magiccreative | C++ Forum | 1 | 06-Feb-2004 08:17 |
| a C input question.. | tmike | C Programming Language | 2 | 19-Sep-2003 03:39 |
| a C input question | tmike | C Programming Language | 1 | 16-Sep-2003 03:31 |
| a noobish compiler question | Charunks | C++ Forum | 5 | 03-Sep-2003 03:18 |
| Regular Expressions question. | JdS | MySQL / PHP Forum | 3 | 04-Nov-2002 15:04 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The