简体   繁体   中英

preg_match not working on long regular expressions

I want to match a keyword using preg_match in php. regular expression is working perfectly on www.regexr.com but not in my php code. can someone help. Thankyou.

<?php

$regexx="/[sS]+([\s\t\r]*[\.\~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\[\]\{\}\;\:\"\'\\\|\,\.\<\>\/\?\d\w]*)+[hH]+([\s\t\r]*[\.\~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\[\]\{\}\;\:\"\'\\\|\,\.\<\>\/\?\d\w]*)+[aA]*([\s\t\r]*[\.\~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\[\]\{\}\;\:\"\'\\\|\,\.\<\>\/\?\d\w]*)+[rR]*([\s\t\r]*[\.\~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\[\]\{\}\;\:\"\'\\\|\,\.\<\>\/\?\d\w]*)+[eE]*
/";
if (preg_match($regexx, "S-hare"))
{
    echo 'succeeded';
}
else
{
    echo 'failed';
}

?>

Based on your comment:

I want to match the word 'share' written in any format. for example.. any special character or space in between 'share' would be detected.

I would suggest to simply remove every non alphabetics characters then match what is left.

var_dump($str = preg_replace("/[^a-z]/i", "", "sh@a/#~r:  e !!!"));
var_dump($str === "share");

A raw single regex solution could be:
/[^az]*s[^az]*h[^az]*a[^az]*r[^az]*e[^az]*/i
Which is simple share and [^az]* to match any non alphabetics series of characters between every letters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM