preg-match-all

preg_match_all , get all img tag that include a string

你离开我真会死。 提交于 2020-01-13 05:01:06
问题 this code get all img tags preg_match_all('/<img[^>]+>/i',$a,$page); but I want get tags that their filenames includes "next.gif" or "pre.gif" for example : $page = ' <img border="0" alt="icon" src="http://www.site.com/images/man.gif" width="90" height="90"> <img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v"> <img border="0" alt="icon" src="http://www.site.com/images/2.gif"> <img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90"

preg_match_all - regex to find full urls in string

做~自己de王妃 提交于 2020-01-12 06:17:28
问题 I have spent over 4 hours trying to find a regex patter to my php code without luck. I have a string with html code. It has lot of urls formats like: site*com http://site*com http://www*site*com http://site*com/some.php http://site*om/some.php?var1=1 http://site*com/some.php?var1=1&var2=2 etc. I have the following php code working in part: preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $content, $result, PREG_PATTERN_ORDER); The

preg_replace, str_replace and substr_replace not working in special condition

泪湿孤枕 提交于 2020-01-11 11:38:06
问题 I have the following code: this code finds all html tags in a string and replaces them with [[0]], [[1]] ,[[2]] and so on.(at least that is intented but not workinng); $str = "some text <a href='/review/'>review</a> here <a class='abc' href='/about/'>link2</a> hahaha"; preg_match_all("|<[^>]+>(.*)</[^>]+>|U",$str, $out, PREG_OFFSET_CAPTURE); $count = 0; foreach($out[0] as $result) { $temp=preg_quote($result[0],'/'); $temp ="/".$temp."/"; preg_replace($temp, "[[".$count."]]", $str,1); $count++

How to loop through, match and replace?

丶灬走出姿态 提交于 2020-01-11 11:25:12
问题 I have multiple strings with same curly braces I want to replace them as dynamic if I get the count as 1 then need to replace the first occurrence, If count as 2 then replaces the second occurrence as so on until condition satisfies. <?php include_once("con.php"); $db = new Da(); $con = $db->con(); $String = "{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}"; $Count = 1; if(preg_match_all("/\{\{[^{}]+\}\}/", $lclString, $matches)) { foreach ($matches[0] as $match) { $Count++; $Query =

preg_match_all print *all* matches

我的梦境 提交于 2020-01-11 06:52:54
问题 I need to print all matches using preg_match_all. $search = preg_match_all($pattern, $string, $matches); foreach ($matches as $match) { echo $match[0]; echo $match[1]; echo $match[...]; } The problem is I don't know how many matches there in my string, and even if I knew and if it was 1000 that would be pretty dumb to type all those $match[] 's. 回答1: The $match[0] , $match[1] , etc., items are not the individual matches, they're the "captures". Regardless of how many matches there are, the

php preg_match_all numbers in parentheses

心已入冬 提交于 2020-01-09 11:55:19
问题 I am trying to load a remote website and get all numbers that are inside of parentheses. But what ends up happening is it only matches the last value. Is my regex wrong? Am I using the correct flags? I have added the example of what it should match on in the second $html variable. //$html = file_get_contents("http://example.com/test.html"); $html = "(1234) (12) (1) \r\n (1346326)"; preg_match_all("^[(\d)]+$^", $html, $matches, PREG_PATTERN_ORDER); print_r($matches); echo "<br>"; foreach (

Regex PRCE PHP preg_match_all: How do remove empty nodes in matches array?

戏子无情 提交于 2020-01-06 16:02:50
问题 $text = 'Lorem Ipsum'; $re = '/(?<AA>Any)|(?<BB>Lorem)/ui'; $nMatches = preg_match_all($re, $text, $aMatches); $aMatches will contain the following: Array ( [0] => Array ( [0] => Lorem ) [AA] => Array ( // do not include to result matches array [0] => // because have not match for this part ) [1] => Array ( [0] => ) [BB] => Array ( [0] => Lorem ) [2] => Array ( [0] => Lorem ) ) Question: Is it possible to return the array without nodes for named parts that have no matches? Array ( [0] =>

php preg_match_all kills page for unknown reason

给你一囗甜甜゛ 提交于 2020-01-06 05:49:11
问题 I am trying to validate a bunch of text and check if there are any emails in it... so i use the following code: if (preg_match_all("/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+/", $str, $matches)){ } this will work for TEXT_A in Page1 but when i go in Page2 and try to check again on TEXT_A it will kill the page with "Problem loading page" error... if i remove this check the page will load fine... i dont get why this is happening... edit:im using CodeIgniter 回答1: I see a

Get number from a string

北城以北 提交于 2020-01-03 19:34:31
问题 I have a string for example "lorem 110 ipusm" and I want to get the 110 I already tried this: preg_match_all("/[0-9]/", $string, $ret); but this is returning this: Array ( [0] => 1 [1] => 1 [2] => 0 ) I want something like this Array ( [0] => 110 ) 回答1: To catch any floating point number use: preg_match_all("/[+-]?\d+[\d\.Ee+]*/", $string, $matches); for example: <? $string = 'ill -1.1E+10 ipsum +1,200.00 asdf 3.14159, asdf'; preg_match_all("/[+-]?\d+[\d\.Ee+]*/", $string, $matches); var_dump

PHP 7 regex not working the same way as in 5

守給你的承諾、 提交于 2020-01-02 07:06:21
问题 So I have to use php 7 and I have the following regex which works just fine in 5.5 and 5.6. ([\\'\\\"].*[\\'\\\"])([\\s]*[\\s]*=>[\\s]*[\\s])(\\[([^\\[\\]]|(?R))*[\\s]*[\\s]\\])/m when I run this in any version of 5 with preg_match_all I get the correct result. Basically I am trying to match an array in a text file of the form 'key' => [ 'val1 => 'sdsd', 'val2' => '3e3', ] The above expression selects this array. In PHP 7 (7.0.8 and 7.0.9) returns no matches what so ever. Anyone has any ideas