Jump to content

[SOLVED] Convert to Italics


raku

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/100985-solved-convert-to-italics/
Share on other sites

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.