Home > Backend Development > PHP Tutorial > 求好手帮忙写一个截取字符串的正则表达式

求好手帮忙写一个截取字符串的正则表达式

WBOY
Release: 2016-06-13 13:17:21
Original
825 people have browsed it

求高手帮忙写一个截取字符串的正则表达式
完全不懂正则,但最近一个小事情需要用到,求高手帮忙

目的是实现从html代码中,截取相关数字:

html代码如下,要截取的数字用红色标记



------解决方案--------------------
也不知道你那逗号到底是要还是不要...还是一个要一个不要...

PHP code

$str = <input type="hidden" id="firstidarticle" name="firstidarticle" value="2425898"><input type="hidden" name="pageflag" value="1">  
HTML;

if (preg_match('/idArticleslist"\svalue="([\d\,]+)"/', $str, $match)) {
    echo $match[1];
}
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
PHP code
[liangdong@bb-browser-test00.vm.baidu.com php_project]$ php main.php 
Array
(
    [0] => 2425898
    [1] => 2427913
)
[liangdong@bb-browser-test00.vm.baidu.com php_project]$ cat main.php 
<?php $str = <<<EOF
<input type="hidden" name="idArticleslist" value="2425898,2427913,"><input type="hidden" id="firstidarticle" name="firstidarticle" value="2425898"><input type="hidden" name="pageflag" value="1">  
EOF;

$n = preg_match('/<input. if print_r trim>
<br><font color="#e78608">------解决方案--------------------</font><br><dl class="code">PHP code<pre class="brush:php;toolbar:false">
$s=<input type="hidden" id="firstidarticle" name="firstidarticle" value="2425sss98"><input type="hidden" name="pageflag" value="1">  
html;
preg_match('#(?<font color="#e78608">------解决方案--------------------</font><br><input type="hidden" name="idArticleslist" value="2425898,2427913,"><br>正则:<br><input type='\"hidden\"' name='\"idArticleslist\"' value='\"(.[^"]*)\"'><br>正则就这么简单,需要转译的加一个反斜线,要匹配的都在括号里,防止匹配的太贪婪,你就告诉他[^"],意思是遇到双引号就匹配结束.这样就行了<br>给你推荐一个软件 RegexBuddy  这个软件用来测试正则表达式的,你可以借助它学一下正则,写程序不会正则是很麻烦的. <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template