在给定的页面上有许多元素:
<div class="some class"> <-- here is anything, other divs, even other divs with
the same class, but I need to match right on closing tag for this particular
opening tag --></div>
Run Code Online (Sandbox Code Playgroud) 我有一点问题,因为我的preg_match_all运行不正常.
我想要做的是从wordpress中提取post_content 中所有图像的src参数,这是一个字符串 - 而不是一个完整的html文档/ DOM(因此不能使用文档解析器函数)
我目前正在使用下面的代码,不幸的是它太不整洁,仅适用于1个图像src,我想要来自该字符串的所有图像源
preg_match_all( '/src="([^"]*)"/', $search->post_content, $matches);
if ( isset( $matches ) )
{
foreach ($matches as $match)
{
if(strpos($match[0], "src")!==false)
{
$res = explode("\"", $match[0]);
echo $res[1];
}
}
}
Run Code Online (Sandbox Code Playgroud)
谁可以请帮助...
我试图破解html并在<tr>标签之间抓取物品.由于某些原因,某些标签以大写形式出现(<TR>)并且被我的模式忽略.我怎么能告诉我的模式忽略大小写.
我目前的模式是:
preg_match_all("|<tr(.*)</tr>|U",$table,$rows);
Run Code Online (Sandbox Code Playgroud) 我preg_match_all($pattern, $string, $matches)经常使用数据,基本上,我总是需要从数组中删除0.00英镑或0.00美元或0.00欧元.
如果有的话,我永远不知道它将在阵列中的位置.
我尝试过unset($matches['£0.00']),unset($matches[0]['£0.00'])但没有奏效
此外,它可以在不使用实际货币符号和正则表达式的情况下完成 \p{Sc}
所以,总结一下,我有一个带货币符号的数字数组,需要删除任何零条目.
样本数组:
Array ( [0] => Array ( [0] => £18.99 [1] => £0.00 [2] => £0.00 [3] => £0.00 ) )
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我想在文本中搜索单词"session".但我想检索出现这个词的整行.到目前为止,我已经想出了这个.
$pattern="[^\\n]*session[^\\n]*";
preg_match_all($pattern,$content, $matches, PREG_OFFSET_CAPTURE);
Run Code Online (Sandbox Code Playgroud)
但是我收到错误"Unknown modifier'*'".任何想法如何制作这样的正则表达式?
我的代码如下:
preg_match_all(UNKNOWN, "I need \"this\" and 'this'", $matches)
Run Code Online (Sandbox Code Playgroud)
我需要REGEX $matches只返回两个没有引号的"this"条目.
可以从字符串中获取嵌套花括号中的所有内容吗?例如:
敏捷的棕色狐狸跳过了懒狗
所以我需要:
从大多数嵌套的顺序来看,这个顺序更好.
我有这个子模式:
<?php
$wavs = 'aaaa="" wav="d" bbbbb="" wav="gerg" ccccc="" wav="" ddddd=""';
preg_match_all('#(?<=wav=").+?(?=")#', $wavs, $matches);
print_r($matches);
?>
Run Code Online (Sandbox Code Playgroud)
它导致此输出:
php test.php
Array
(
[0] => Array
(
[0] => d
[1] => gerg
[2] => " ddddd=
)
)
Run Code Online (Sandbox Code Playgroud)
虽然我预计只有2场比赛:
php test.php
Array
(
[0] => Array
(
[0] => d
[1] => gerg
)
)
Run Code Online (Sandbox Code Playgroud)
这是什么问题?为什么要捕获额外无关的字符串?
编辑:( M42响应)
preg_match_all('#(?<=wav=").*?(?=")#', $wavs, $matches);
Run Code Online (Sandbox Code Playgroud)
仍导致不正确的匹配:
Array
(
[0] => Array
(
[0] => d
[1] => gerg
[2] =>
[3] => " ddddd=
) …Run Code Online (Sandbox Code Playgroud) 我需要解析以下文本:
First: 1
Second: 2
Multiline: blablablabla
bla2bla2bla2
bla3b and key: value in the middle if strting
Fourth: value
Run Code Online (Sandbox Code Playgroud)
Value是一个字符串OR多行字符串,同时值可以包含"key:blablabla"substring.应该忽略这样的子字符串(不解析为单独的键值对).
请帮我使用正则表达式或其他算法.
理想的结果是:
$regex = "/SOME REGEX/";
$matches = [];
preg_match_all($regex, $html, $matches);
// $mathes has all key and value parsed pairs, including multilines values
Run Code Online (Sandbox Code Playgroud)
谢谢.
我尝试使用简单的正则表达式,但结果不正确,因为我不知道如何处理多行:
$regex = "/(.+?): (.+?)/";
$regex = "/(.+?):(.+?)\n/";
...
Run Code Online (Sandbox Code Playgroud) 我试图获得点后的值,我想得到所有这些(每个都是他们自己的键/值).
以下是我正在运行的内容:
$string = "div.cat.dog#mouse";
preg_match_all("/\.(.+?)(\.|#|$)/", $string, $matches);
Run Code Online (Sandbox Code Playgroud)
当我做转储$matches我得到这个:
Array
(
[0] => Array
(
[0] => .cat.
)
[1] => Array
(
[0] => cat
)
[2] => Array
(
[0] => .
)
)
Run Code Online (Sandbox Code Playgroud)
在item的[1]位置,它只返回1个值.我期待的是它返回(对于这种情况)2项cat和dog.怎么dog没被捡到preg_match_all?
php ×10
preg-match-all ×10
regex ×9
preg-match ×3
html ×1
line ×1
parsing ×1
web-scraping ×1
wordpress ×1