raku Posted April 14, 2008 Share Posted April 14, 2008 Hi, I'm using the pattern '/_(.+)_/Ui' to match words/phrases surrounded with underscores and convert them to html italics. But, I would like to avoid links that may have double underscores in them. Is there a way to make the pattern only match if there is whitespace or nothing on either side of the underscores? Thanks. Quote Link to comment Share on other sites More sharing options...
aCa Posted April 14, 2008 Share Posted April 14, 2008 To only match the values with whitespace on each side you could use this pattern: /\s_(.+)_\s/iU Would that solve your problem? I have a beta out of my regex tool that can help you test the expression to make sure it is exactly what you wan't, http://regex.larsolavtorvik.com/. Quote Link to comment Share on other sites More sharing options...
raku Posted April 14, 2008 Author Share Posted April 14, 2008 Thanks. The only problem there is that it doesn't match near the end of sentences. For example: "This is _cool_." won't italicize "cool" So essentially, I would also like it to match if there is nothing on the right of the word/phrase or if there is punctuation. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 14, 2008 Share Posted April 14, 2008 Try /\b_(.+)_\b/iU. Quote Link to comment Share on other sites More sharing options...
raku Posted April 14, 2008 Author Share Posted April 14, 2008 Thanks, it works for that example. But, for the url http://www.google.com/_testing/blah_.html it converts the underscores into spaces and makes testing/blah italic. Quote Link to comment Share on other sites More sharing options...
Orio Posted April 14, 2008 Share Posted April 14, 2008 Can you show the whole code... effigy posted a pattern, not a preg_replace statement. Orio. Quote Link to comment Share on other sites More sharing options...
raku Posted April 14, 2008 Author Share Posted April 14, 2008 Sure. <?php $subs = array( '/\b_(.+)_\b/iU' => ' <em>$1</em> ' ); $text = "http://www.google.com/_testing/blah_.html"; $formatted_text = preg_replace(array_keys($subs), array_values($subs), $text); ?> Thanks. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 14, 2008 Share Posted April 14, 2008 I would like to avoid links that may have double underscores in them. I assume by "double underscores" you mean any sets (pairs) within a URL? So, essentially, you want to avoid links altogether, correct? Quote Link to comment Share on other sites More sharing options...
Orio Posted April 14, 2008 Share Posted April 14, 2008 How do you want the output (exactly) to look like? Orio. EDIT- lol nvm. I'll leave it to the master Quote Link to comment Share on other sites More sharing options...
raku Posted April 14, 2008 Author Share Posted April 14, 2008 Effigy: Yeah that's exactly it. Quote Link to comment Share on other sites More sharing options...
raku Posted April 14, 2008 Author Share Posted April 14, 2008 Orio: For that string, I don't want anything to happen to it. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 14, 2008 Share Posted April 14, 2008 You'll need something similar to this, which was used to ignore HTML: <pre> <?php $data = <<<DATA _italic_http://www.phpfreaks.com http://www.google.com/_do_not_italicize _abc__123_xyz DATA; ### Isolate the URLs from the rest of the data. $pieces = preg_split('%(http://\S+)%', $data, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); ### Analyze each piece. foreach ($pieces as &$piece) { ### Ignore the URLs. if (substr($piece, 0, 7) == 'http://') { continue; } $piece = preg_replace('/_([^_]+)_/', '<em>$1</em>', $piece); } ### Reassemble. echo $data = join('', $pieces); ?> </pre> Quote Link to comment Share on other sites More sharing options...
raku Posted April 14, 2008 Author Share Posted April 14, 2008 Awesome, thanks so much! Quote Link to comment Share on other sites More sharing options...
raku Posted April 19, 2008 Author Share Posted April 19, 2008 Quick question: what does the "&" sign do in "&$piece"? Thanks. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 21, 2008 Share Posted April 21, 2008 It passes by reference, allowing $piece to be modified by the loop. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.