note 112055 added to function.preg-match

From: Date: Fri, 26 Apr 2013 13:29:04 +0000
Subject: note 112055 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-194640@lists.php.net to get a copy of this message
===example 7)===: to search for ===special characters=== (like \/".<*'? and etc..) inside preg_match, you need to use this escape sign: \ <?php $text = 'hello Jimmy/Kroger '; preg_match("/Jimmy\/Kroger/",$text,$out); echo $out[0]; ?> now, we can use ^ operator, which searches for results conversely. ===example 8)===: find everything rather than letters and numbers: <?php $text = 'abc@*&^)($%'; preg_match_all('/[^a-zA-Z0-9.]/',$text,$out); foreach ($out[0] as $varr) {echo $varr;} ?> output will be: @*&^)($% for SEARCH and REPLACE, we have a bit different structure, as we need to use the new variable. ===example 9)===: find and replace everything rather than letters and numbers (now, we need this operator ^). <?php $text = 'ab2sq)(&*(%$%^$@%n23f9'; $variable = preg_replace('/[^a-zA-Z0-9.]/','a',$text); echo $variable; ?> output will be: ab2sqn23f9 ===example 10)===: search and add something inside the found results: <?php $text = 'Hi, its me, Niko from Austria'; $variable = preg_replace('/(Niko.*?) from/', '$1 Gomez', $text); echo $variable; ?> output will be : its me, Niko Gomez Austria ===example 11)===: find all links inside text (at first, modify link, as i cant post here the http:// normally): <?php $text = 'hi, my site is http--example.com/blabla.html and another page is http--domain2.com/page37.php'; preg_match_all("/[[:alpha:]]+:\/\/[^<>[:space:]]+[[:alnum:]\/]/",$text, $out); foreach($out[0] as $varr){echo $varr;} ?> output will be: http;//example.com/blabla.html http;//domain2.com/page37.php ===example 12)===: like the example 11 (but with replace) - find links in text and put them in anchored tags: <?php $text = 'hi, my site is http--example.com/blabla.html and another page is http--domain2.com/page37.php'; $variable = preg_replace("/[[:alpha:]]+:\/\/[^<>[:space:]]+[[:alnum:]\/]/",'<a href="\\0">\\0</a>', $text); echo $variable; ?> output will be the same sentence, but the links will be anchored. 1) *Tips: Do not use preg_match() if you only want to check if one string is contained in another string. Use stristr() or strpos() instead as they will be faster.* 2) ===more advanced, specific examples about php regular expressions, use google, or see === FULL OPTIONS AND MANUALS at - www.php.net/manual/en/reference.pcre.pattern.syntax.php ( You can review shortly all operators list here - www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers www.noupe.com/php/php-regular-expressions.html ) 3) for hmtl codes, there exist special light php app, called- Dom Parser. but sometimes, if you know php regular expressions well, you might not need dom parser. ---- Server IP: 69.147.83.199 Probable Submitter: 149.3.87.197 ---- Manual Page -- http://php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/112055 Del: integrated -- https://master.php.net/note/delete/112055/integrated Del: useless -- https://master.php.net/note/delete/112055/useless Del: bad code -- https://master.php.net/note/delete/112055/bad+code Del: spam -- https://master.php.net/note/delete/112055/spam Del: non-english -- https://master.php.net/note/delete/112055/non-english Del: in docs -- https://master.php.net/note/delete/112055/in+docs Del: other reasons-- https://master.php.net/note/delete/112055 Reject -- https://master.php.net/note/reject/112055 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194640) next »