我对正则表达式很陌生.你能帮我创建一个匹配整个单词的模式,包含特定的部分吗?举例来说,如果我有一个文本字符串"执行正则表达式匹配",如果我搜索快车,它shuld给我表达,我如果搜索形式,它应该给我执行等.知道了吗?
可能重复:
将ereg表达式转换为preg
<?php
$searchtag = "google";
$link = "http://images.google.com/images?hl=de&q=$searchtag&btnG=Bilder-Suche&gbv=1";
$code = file_get_contents($link,'r');
ereg("imgurl=http://www.[A-Za-z0-9-]*.[A-Za-z]*[^.]*.[A-Za-z]*", $code, $img);
ereg("http://(.*)", $img[0], $img_pic);
echo '<img src="'.$img_pic[0].'" width="70" height="70">'; ?>
Run Code Online (Sandbox Code Playgroud)
我得到这个错误
不推荐使用:函数ereg()在第5行的C:\ Program Files\EasyPHP-5.3.8.1\www\m\img.php中已弃用
不推荐使用:函数ereg()在第6行的C:\ Program Files\EasyPHP-5.3.8.1\www\m\img.php中已弃用
preg_match()函数给出了这个错误
警告:preg_match()[function.preg-match]:在第6行的C:\ Program Files\EasyPHP-5.3.8.1\www\m\img.php中,分隔符不能是字母数字或反斜杠
警告:preg_match()[function.preg-match]:在第7行的C:\ Program Files\EasyPHP-5.3.8.1\www\m\img.php中,分隔符不能是字母数字或反斜杠
您好我想在PHP中使用preg_match来解析html文档中的"Desired text"
<p class="review"> Desired text </p>
Run Code Online (Sandbox Code Playgroud)
通常我会使用simple_html_dom这样的东西,但在这种情况下它不能使用(上面的元素没有出现在每个所需的div标签中,所以我被迫使用这种方法来准确跟踪它何时没有出现和然后相应地从simple_html_dom调整我的数组).
无论如何,这将解决我的问题.
非常感谢.
我使用以下命令运行以下脚本php.exe:
preg_replace('#(?:^[^\pL]*)|(?:[^\pL]*$)#u','',$string);
Run Code Online (Sandbox Code Playgroud)
或等同物:
preg_replace('#(?:^[^\pL]*|[^\pL]*$)#u','',$string);
Run Code Online (Sandbox Code Playgroud)
如果$string="S"或$string=" ?? " 它有效,如果 string='?'它产生?不正确,并且string='??'PHP崩溃.
但它适用于4.4.0 - 4.4.9,5.0.5 - 5.1.6版本.
怎么了 ?
见:http://3v4l.org/T3rpV
<?php
$string='??';
echo preg_replace('#(?:^[^\pL]*)|(?:[^\pL]*$)#u','',$string);
Run Code Online (Sandbox Code Playgroud)
输出为5.4.0 - 5.5.0alpha6
Run Code Online (Sandbox Code Playgroud)Process exited with code 139.输出为5.2.0 - 5.3.22,5.5.0beta1
输出为4.4.0 - 4.4.9,5.0.5 - 5.1.6
Run Code Online (Sandbox Code Playgroud)??输出为4.3.11,5.0.0 - 5.0.4
Run Code Online (Sandbox Code Playgroud)Warning: preg_replace(): Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 7 in /in/T3rpV on line 3输出为4.3.0 …
不使用u标志可以使用的十六进制范围是[\x{00}-\x{ff}],但是使用u标志它可以达到一个4字节的值\x{7fffffff}([\x{00000000}-\x{7fffffff}]).
所以,如果我执行以下代码:
preg_match("/[\x{00000000}-\x{80000000}]+/u", $str, $match);
Run Code Online (Sandbox Code Playgroud)
会得到这个错误:
Warning: preg_match(): Compilation failed: character value in \x{...} sequence is too large
Run Code Online (Sandbox Code Playgroud)
所以我无法匹配类似于 with equivalent hex value of f0 a1 83 81.问题不在于如何匹配这些字母,而是这个范围和这个边界如何来自u修饰符应该将字符串视为UTF-16
echo PCRE_VERSION;
Run Code Online (Sandbox Code Playgroud)
PCRE版本与PHP 5.3.24 - 5.3.28,5.4.14 - 5.5.7:
8.32 2012-11-30
Run Code Online (Sandbox Code Playgroud)
PCRE版本与PHP 5.3.19 - 5.3.23,5.4.9 - 5.4.13:
8.31 2012-07-06
Run Code Online (Sandbox Code Playgroud)
从something@gmail.com我想要获取域名的电子邮件地址gmail.com.我想在Javascript中的文本框值上使用该模式.
Pease建议我为上述要求提供优化且更快的preg模式...
如果字符串是PHP中的正则表达式或普通字符串,是否有一种好的测试方法?
理想情况下,我想编写一个函数来运行字符串,返回true或false.
我看了看preg_last_error():
<?php
preg_match('/[a-z]/', 'test');
var_dump(preg_last_error());
preg_match('invalid regex', 'test');
var_dump(preg_last_error());
?>
Run Code Online (Sandbox Code Playgroud)
显然,第一个不是错误,第二个是错误.但两次preg_last_error()返回int 0.
有任何想法吗?
$str = "This is a string";
$words = explode(" ", $str);
Run Code Online (Sandbox Code Playgroud)
工作正常,但空间仍然进入数组:
$words === array ('This', 'is', 'a', '', '', '', 'string');//true
Run Code Online (Sandbox Code Playgroud)
我更喜欢只有没有空格的单词,并保留有关空格数量的信息.
$words === array ('This', 'is', 'a', 'string');//true
$spaces === array(1,1,4);//true
Run Code Online (Sandbox Code Playgroud)
刚添加:(1, 1, 4)表示第一个单词后面的一个空格,第二个单词后面的一个空格和第三个单词后面的4个空格.
有什么方法可以快速完成吗?
谢谢.
我想要一个解决方案只验证域名不是完整的URL,以下示例是我正在寻找的:
domain.com -> true
domain.net -> true
domain.org -> true
domain.biz -> true
domain.co.uk -> true
sub.domain.com -> true
domain.com/folder -> false
domµ*$ain.com -> false
Run Code Online (Sandbox Code Playgroud)
谢谢
如何检查字符串是否在PHP中有字母表
的而ctype_alpha和 ctype_digit不会在它的帮助.他们的任何方法?
preg-match ×10
php ×9
regex ×7
arrays ×1
crash ×1
ereg ×1
explode ×1
javascript ×1
parsing ×1
preg-replace ×1
string ×1