You are here: Re: Trying to prevent duplicates in preg_replace Regex « PHP Programming Language « IT news, forums, messages
Re: Trying to prevent duplicates in preg_replace Regex

Posted by Toby A Inkster on 02/23/07 22:37

erikcw wrote:

> I'm trying to write a regex pattern to use in preg_replace. Basically
> I want to put [brackets] around every line (\n) in this variable.
> However, I need to exclude lines that already have brackets or
> quotation marks around them as well as lines that start with a
> hyphen. Lastly the lines with ** need the brackets before the **,
> instead of at the end of the line.

$x ='...'; // orig string

$x = preg_replace('/^(.*)$/m', '[$1]', $x);
$x = preg_replace('/^\[\"(.*)\"\]$/m', '"$1"', $x);
$x = preg_replace('/^\[\[(.*)\]\]$/m', '[$1]', $x);
$x = preg_replace('/^\[(.*)(\s*\*\*.*)\]$/m', '[$1]$2', $x);
$x = preg_replace('/^\[\]/m', '', $x);

That last regexp covers the behaviour of empty lines, and lines that start
with a double-asterisk.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация