标签: preg-match-all

使用正则表达式从HTML文档中的链接中提取URL

我需要捕获给定html中的所有链接.

这是示例代码:

<div class="infobar">
    ... some code goes here ...
    <a href="/link/some-text">link 1</a>
    <a href="/link/another-text">link 2</a>
    <a href="/link/blabla">link 3</a>
    <a href="/link/whassup">link 4</a>
    ... some code goes here ...
</div>
Run Code Online (Sandbox Code Playgroud)

我需要把所有环节中div.infobar,与开始/link/

我试过这个:

preg_match_all('#<div class="infobar">.*?(href="/link/(.*?)") .*?</div>#is', $raw, $x);
Run Code Online (Sandbox Code Playgroud)

但它给了我唯一的第一场比赛.

谢谢你的建议.

php regex preg-match-all

3
推荐指数
1
解决办法
2360
查看次数

正则表达式,用于检测以星号开头的行

下面的代码无法检测出现的单个实例.= O什么错了?= \

如何检测以星号开头的以下行(以换行符开头)?我不知所措.这不符合我的预期.

$text ="Nothing here to detect...though it is the first line.
* '' [[test]]
* Another line that starts with an asterisk 
** yet another...though it has two...but who cares about the 2nd one?";


$t = preg_match_all('#^\*.*#', $text, $match);
echo "found=".$t."\n";
print_r($match);
Run Code Online (Sandbox Code Playgroud)

php regex preg-match-all preg-match

3
推荐指数
1
解决办法
423
查看次数

在PHP中将数字序列转换为数组

有点在PHP和Regex中的noobie,我从Web服务收到以下内容:

test:002005@1111@333333@;10205@2000@666666@;002005@1111@55555@;
Run Code Online (Sandbox Code Playgroud)

上面的行是3个数字的序列,重复3次.我想获得每个序列的第3个数字,我相信最好的课程(除了3000次爆炸)将是preg_match_all但我在艰难的时间环绕着RegEx.

最终结果应如下所示:

 Array
    (
        [0] => 333333
        [1] => 666666
        [2] => 55555
    )
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

php regex arrays string preg-match-all

3
推荐指数
1
解决办法
321
查看次数

为什么preg_match_all()多次创建相同的答案?

以下代码从推文中提取#hashtags并将它们放在变量$ matches中.

$tweet = "this has a #hashtag a  #badhash-tag and a #goodhash_tag";

preg_match_all("/(#\w+)/", $tweet, $matches);

var_dump( $matches );
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释为什么以下结果有2个相同的数组而不是1个

array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(8) "#hashtag"
    [1]=>
    string(8) "#badhash"
    [2]=>
    string(13) "#goodhash_tag"
  }
  [1]=>
  array(3) {
    [0]=>
    string(8) "#hashtag"
    [1]=>
    string(8) "#badhash"
    [2]=>
    string(13) "#goodhash_tag"
  }
}
Run Code Online (Sandbox Code Playgroud)

php preg-match-all

3
推荐指数
1
解决办法
137
查看次数

正则表达式,用于在标签之间提取文本,但不提取标签

我想写一个正则表达式,它提取<title>字符串中两个标签之间的内容,但不提取标签.IE我有以下内容

<title>My work</title>
<p>This is my work.</p> <p>Learning regex.</p>
Run Code Online (Sandbox Code Playgroud)

正则表达式

(<title>)(.*?)(<\/title>)
Run Code Online (Sandbox Code Playgroud)

提取物,<title>My work</title>但我只想提取My work.我怎样才能做到这一点?这是http://regex101.com/r/mD8fB0示例的链接

php regex preg-match-all preg-match

3
推荐指数
3
解决办法
2万
查看次数

用于提取没有空格的哈希标记的正则表达式

我正在使用这个:

$t = "#hashtag #goodhash_tag united states #l33t this";
$queryVariable = "";
if(preg_match_all('/(^|\s)(#\w+)/', $t, $arrHashTags) > 0){
    array_filter($arrHashTags);
    array_unique($arrHashTags);
    $count = count($arrHashTags[2]);
    if($count > 1){
        $counter = 1;
        foreach ($arrHashTags[2] as $strHashTag) {
            if (preg_match('/#\d*[a-z_]+/i', $strHashTag)) {
                if($counter == $count){
                    $queryVariable .= $strHashTag;              
                } else{
                    $queryVariable .= $strHashTag." and ";
                }
                $newTest = str_replace($arrHashTags[2],"", $t);                 
            }
            $counter = $counter + 1;
        }
    }
}
echo $queryVariable."<br>"; // this is list of tags
echo $newTest;   // this is the remaining text …
Run Code Online (Sandbox Code Playgroud)

php regex preg-match-all preg-match

3
推荐指数
1
解决办法
596
查看次数

Php Regex查找string是否为mysql select语句

我正在尝试在执行它们之前验证查询,如果查询不是mysql select语句,那么我必须向用户显示消息.

我在此链接中找到了以下正则表达式: 使用正则表达式验证简单选择查询

$reg="/^Select\s+(?:\w+\s*(?:(?=from\b)|,\s*))+from\s+\w+\s+where\s+\w+\s*=\s*'[^']*'$/i"; 
Run Code Online (Sandbox Code Playgroud)

接下来我写下面的代码,但它总是打印不选择查询($ match每次都是空的)

$string="select * from users where id=1";
preg_match_all($reg,$string,$match);
if(!empty($match)){
echo "select query";
//execute and process result
//$this->user_model->list($string);
}else{
   echo "not select query";
   //show_message('inv_query');
}
Run Code Online (Sandbox Code Playgroud)

请更正正则表达式以验证sql select语句(select,from,where,join,orderby groupby all in select statement).或者让我知道完成任务的其他好方法.

/*
some sample select statements
select * from users where id=1;

select * from users where id=1 AND name= 'Prabhu';

select * from users where id=1 AND name= 'Prabhu' order by name;

Select * from users where id=1 AND name= 'Prabhu' group by …
Run Code Online (Sandbox Code Playgroud)

php regex preg-match-all preg-match

3
推荐指数
1
解决办法
2214
查看次数

正则表达式,带有preg_match_all的模式中的反向引用问题

我想知道这里的反向引用有什么问题:

preg_match_all('/__\((\'|")([^\1]+)\1/', "__('match this') . 'not this'", $matches);
Run Code Online (Sandbox Code Playgroud)

它应该匹配__('')之间的字符串,但实际上它返回:

match this') . 'not this
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

php regex backreference preg-match-all

2
推荐指数
1
解决办法
2757
查看次数

使用PHP中的正则表达式将多行字符串转换为多元素数组

我需要拆分以下字符串并将每个新行放入一个新的数组元素中.

this is line a.(EOL chars = '\r\n' or '\n')
(EOL chars)
this is line b.(EOL chars)
this is line c.(EOL chars)
this is the last line d.(OPTIONAL EOL chars)
Run Code Online (Sandbox Code Playgroud)

(请注意,最后一行可能没有任何EOL字符.字符串有时也只包含1行,根据定义,它是最后一行.)

必须遵循以下规则:

  • 应丢弃空行(如第二行),不要将其放入数组中.
  • 不应包含EOL字符,否则我的字符串比较会失败.

所以这应该导致以下数组:

[0] => "this is line a."
[1] => "this is line b."
[2] => "this is line c."
[3] => "this is the last line d."
Run Code Online (Sandbox Code Playgroud)

我尝试过以下操作:

$matches = array();
preg_match_all('/^(.*)$/m', $str, $matches);
return $matches[1];
Run Code Online (Sandbox Code Playgroud)

$ matches [1]确实包含每个新行,但是:

  • 还包括空行
  • 似乎一个'\ r'字符无论如何都会在数组中的字符串末尾被走私.我怀疑这与正则表达式范围有关.' 其中包括除'\n'以外的所有内容.

无论如何,我一直在玩'\ R'和诸如此类的东西,但我找不到符合我上面概述的两条规则的好的正则表达式.有什么帮助吗?

php regex newline preg-match-all

2
推荐指数
1
解决办法
2462
查看次数

获取所有嵌套的花括号

可以从字符串中获取嵌套花括号中的所有内容吗?例如:

敏捷的棕色狐狸跳过了懒狗

所以我需要:

  • 过了
  • 跳过{懒惰}

从大多数嵌套的顺序来看,这个顺序更好.

php regex preg-match-all preg-match

2
推荐指数
1
解决办法
2730
查看次数

标签 统计

php ×10

preg-match-all ×10

regex ×9

preg-match ×5

arrays ×1

backreference ×1

newline ×1

string ×1