草庐IT

php - 警告 : preg_replace(): No ending delimiter '/' found

这个问题在这里已经有了答案:WhatDelimitertouseforpreg_replaceinPHP(replaceworkingoutsideofPHPbutnotinside)(3个答案)关闭7年前。警告:preg_replace():Noendingdelimiter'/'foundinC:\wamp\www\upload\upload_demo.phponline77我使用preg_replace()将斜杠替换为反斜杠。但它显示了上述警告。这是代码..functiondel_file($file){$delete=@unlink($file);clearstatcache(

php - 有效模式的 PHP 中的 "preg_match(): Compilation failed: unmatched parentheses"

想知道是否有人可以阐明为什么在PHP的preg_match函数中使用以下正则表达式会失败:-这会导致错误消息“preg_match():编译失败:括号不匹配”,尽管该模式似乎是有效的。我用在线测试了它PHPRegularExpressiontester和Linux工具Kiki。似乎PHP正在转义左括号而不是反斜杠。我通过使用str_replace将反斜杠换成正斜杠来解决这个问题。这适用于我的情况,但很高兴知道为什么这个正则表达式失败。 最佳答案 要对文字反斜杠进行编码,您需要将其转义两次:一次用于字符串,一次用于正则表达式引擎:pr

php - 从字符串中获取电子邮件 - 正则表达式语法 + preg_match_all

这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭9年前。我正在尝试从字符串中获取电子邮件:$string="blablapickachu@domain.comMIME-Version:balbasur@domain.comblablabla";$matches=array();$pattern='\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b';preg_match_all

php - preg_match : nothing to repeat/no match

我正在使用这个:if(!preg_match('/^+[0-9]$/','+1234567'))并且得到:Warning:preg_match()[function.preg-match]:Compilationfailed:nothingtorepeatatoffset1有什么想法吗?更新:现在使用这个:if(!preg_match('/^\+[0-9]$/','+1234567'))我找不到匹配项。有什么想法吗? 最佳答案 +是一个特殊字符,表示前一个字符的1个或多个,不转义它就是将它应用于插入符号。用\转义它,它将匹配文字加号

PHP preg_match 圣经经文格式

我正在努力构建一个正则表达式来解析这种字符串(圣经经文):'John14:16–17,25–26''John14:16–17''John14:16''John14''John'所以基本模式是:书籍[[Chapter][:Verse]]章节和诗歌是可选的。 最佳答案 我认为这可以满足您的需求:\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})?假设:数字总是以1位或2位数字为一组破折号将匹配以下任一-和–下面是带有注释的正则表达式:"\w#Matchasin

php - 使用 preg_replace() 将 CamelCase 转换为 snake_case

我现在有一个方法可以将我的驼峰式字符串转换为蛇形式,但它被分成三个调用preg_replace():publicfunctioncamelToUnderscore($string,$us="-"){//inserthyphenbetweenanyletterandthebeginningofanumericchain$string=preg_replace('/([a-z]+)([0-9]+)/i','$1'.$us.'$2',$string);//inserthyphenbetweenanylower-to-upper-caseletterchain$string=preg_repl

PHP preg_match 查找多次出现

在PHP中使用preg_match查找同一字符串多次出现的正则表达式的正确语法是什么?例如,查找以下字符串是否在以下段落中出现两次:$string="/brownfoxjumped[0-9]/";$paragraph="Thebrownfoxjumped1timeoverthefence.Thegreenfoxdidnot.Thenthebrownfoxjumped2timesoverthefence"if(preg_match($string,$paragraph)){echo"matchfound";}else{echo"matchNOTfound";}

php - ereg_replace 到 preg_replace?

我怎样才能转换ereg_replace(".*\.(.*)$","\\1",$imgfile);到preg_replace...??我有问题吗? 最佳答案 您应该知道将ereg模式移植到preg的4个主要事项:添加分隔符(/):'pattern'=>'/pattern/'转义分隔符如果它是模式的一部分:'patt/ern'=>'/patt\/ern/'通过以下方式以编程方式实现它:$ereg_pattern='.+';$preg_pattern='/'.addcslashes($ereg_pattern,'/').'/';eregi

php - 使用 preg_replace 时如何增加替换字符串中的计数?

我有这段代码:$count=0;preg_replace('/test/','test'.$count,$content,-1,$count);对于每个替换,我都会得到test0。我想得到test0、test1、test2等。 最佳答案 使用preg_replace_callback():$count=0;preg_replace_callback('/test/','rep_count',$content);functionrep_count($matches){global$count;return'test'.$count++

php - 如何精确匹配 PHP 的 preg_match 中的 3 位数字?

这个问题在这里已经有了答案:`preg_match`theWholeStringinPHP(3个答案)关闭3年前。假设您具有以下值:12312344567121我正在尝试纠正preg_match,它只会为“123”返回true,因此只有3位数字才匹配。这是我的,但它也匹配1234和4567。我可能还有一些东西。preg_match('/[0-9]{3}/',$number);