<?php
$flag=true;
if(isset($_POST['sub'])){
if(isset($_POST['text'])){
$a=$_POST["text"];
} else {
$a='';
}
if(!empty($_POST['msg'])){
$b=$_POST['msg'];
$c=strlen($b);}
if(isset($_POST['wrd'])){
$d=($_POST["wrd"]);
} else {
$d='';
}
if(preg_match("[\w\s.,a-zA-Z$a,\.]",$b)){
$flag=false;
}
if($flag){
$i;
for($i=0;$i<=$c;$i++)
{
$newtext = str_replace($a,$d,$b);
echo $newtext;
echo "</br>";
break;
}
} else {
echo"not found ";}
}
?>
This is my code I want to match word(paragraph) from a original paragraph but the problem is this.
In one line the word (paragraph) is written like this (paragraph,) and (paragraph.)
That's why preg_match is not able to find the these two word and same goes to preg_replace also.
This is incorrect:
if(preg_match("[\w\s.,a-zA-Z$a,\.]",$b))
Regex needs start and end delimiters. It should be:
if(preg_match("/[\w\s.,a-zA-Z$a,\.]/", $b))
Also note that your regex is also incorrect. I can see few mistakes (there myay be more):
inside square brackets you don't need to escape dot
you seem to have another dot that will match ANY character
You have \w that means word character hence you don't need separate a-zA-Z
You have a misplaced a after $ sign
Related
I have a strange question maybe you could help me with. I am trying to check whether the given string contains special characters. The code below is working however one character seems to get exempted on the condition which is the square brackets [ ]. Can you help me with this? thank you.
$string = 'starw]ars';
if (preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-]/', $string)) {
echo 'Output: Contains Special Characters';
}else{
echo 'Output: valid characters';
}
Please note: I can't use below condition since I need to accept others characters from other languages like in arabic, chinese, etc. so it means I need to specify all characters that is not allowed.
if (!preg_match('/[^A-Za-z0-9]/', $string))
Appreciate your help. Thanks.
You forgot to add square brackets [] in your expression. I have added this \[\] in your current expression.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = 'starw]ars';
if (preg_match('/[\[\]\'^£$%&*()}{##~?><>,|=_+¬-]/', $string))
{
echo 'Output: Contains Special Characters';
} else
{
echo 'Output: valid characters';
}
Use strpos:
$string = 'starw]ars';
if (strpos($string, ']') !== false) {
echo 'true';
}
Please see the following answer for additional information:
How do I check if a string contains a specific word in PHP?
You should add escaped square brackets to your expression.
preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-\[\]]/', $string)
EDIT: Apologies to #Thamilan, I didn't see your comment.
EDIT 2: You could also use the preg_quote function.
preg_match(preg_quote('\'^£$%&*()}{##~?><>,|=_+¬-[]', '/'), $string);
The preg_quote function will escape your special characters for you.
Try this example:
<?php
$string = 'starw]]$%ars';
if (preg_match('/[\'\/~`\!##\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $string))
{
echo 'Output: Contains Special Characters';
} else
{
echo 'Output: valid characters';
}
?>
Output:
Output: Contains Special Characters
Sorry for asking this simple question, but I seem not to find any answers.
How can you check whether there is a number within a string?
I've tried:
$string = "this is a simple string with a number 2432344";
if (preg_match('/[^0-9]/', "$string")) {
echo "yes a number";
} else {
echo "no number";
}
Doesn't seem to work...
If you want to find a number, don't negate the character set with ^ in your regular expression. That means "match anything except a number".
$string = "this is a simple string with a number 2432344";
if (preg_match('/[0-9]/', "$string")) {
echo "yes a number";
} else {
echo "no number";
}
Also, you could just use \d instead of [0-9], and $string instead of "$string".
^ will only negate what's in the regex. You don't need to use it.
I would like to validate a string with a pattern that can only contain letters (including letters with accents). Here is the code I use and it always returns "nok".
I don't know what I am doing wrong, can you help? thanks
$string = 'é';
if(preg_match('/^[\p{L}]+$/i', $string)){
echo 'ok';
} else{
echo 'nok';
}
Add the UTF-8 modifier flag (u) to your expression:
/^\p{L}+$/ui
There is also no need to wrap \p{L} inside of a character class.
I don't know if this helps anybody that will check this question / thread later. The code below allows only letters, accents and spaces. No symbols or punctuation like .,?/>[-< etc.
<?php
$string = 'États unis and états unis';
if(preg_match('/^[a-zA-Z \p{L}]+$/ui', $string)){
echo 'ok';
} else{
echo 'nok';
}
?>
If you want to add numbers too, just add 0-9 immediately after Z like this a-zA-Z0-9
Then if you are applying this to form validation and you are scared a client/user might just hit spacebar and submit, just use:
if (trim($_POST['forminput']) == "") {... some error message ...}
to reject the submission.
I'm trying to find special characters with characters like <?, <?php, or ?> in a string. The code below works to find "php" in the string anywhere no matter if it's PHP, php, or phpaPHPa.
<?php
$searchfor = "php";
$string = "PHP is the web scripting language of choice.";
if (preg_match("/".$searchfor."/i", $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
I need a similar code that finds special characters like <?, <?php, or ?> in the string. Any suggestions?
You can use same code. Just make sure to escape regex special characters when using them in matching. The question mark must be escaped so your $searchfor becomes <\?php
Using strpos (or stripos) will be faster than using RegEx'es...
if(false === strpos($text, '<?'))
echo 'Match was not found';
else
echo 'Match was found';
I have a pattern with a small list of words that are illegal to use as nicknames set in a pattern variable like this:
$pattern = webmaster|admin|webadmin|sysadmin
Using preg_match, how can I achieve so that nicknames with these words are forbidden, but registering something like "admin2" or "thesysadmin" is allowed?
This is the expression I have so far:
preg_match('/^['.$pattern.']/i','admin');
// Should not be allowed
Note: Using a \b didn't help much.
What about not using regex at all ?
And working with explode and in_array ?
For instance, this would do :
$pattern = 'webmaster|admin|webadmin|sysadmin';
$forbidden_words = explode('|', $pattern);
It explodes your pattern into an array, using | as separator.
And this :
$word = 'admin';
if (in_array($word, $forbidden_words)) {
echo "<p>$word is not OK</p>";
} else {
echo "<p>$word is OK</p>";
}
will get you
admin is not OK
Whereas this (same code ; only the word changes) :
$word = 'admin2';
if (in_array($word, $forbidden_words)) {
echo "<p>$word is not OK</p>";
} else {
echo "<p>$word is OK</p>";
}
will get you
admin2 is OK
This way, no need to worry about finding the right regex, to match full-words : it'll just match exact words ;-)
Edit : one problem might be that the comparison will be case-sensitive :-(
Working with everything in lowercase will help with that :
$pattern = strtolower('webmaster|admin|webadmin|sysadmin'); // just to be sure ;-)
$forbidden_words = explode('|', $pattern);
$word = 'aDMin';
if (in_array(strtolower($word), $forbidden_words)) {
echo "<p>$word is not OK</p>";
} else {
echo "<p>$word is OK</p>";
}
Will get you :
aDMin is not OK
(I saw the 'i' flag in the regex only after posting my answer ; so, had to edit it)
Edit 2 : and, if you really want to do it with a regex, you need to know that :
^ marks the beginning of the string
and $ marks the end of the string
So, something like this should do :
$pattern = 'webmaster|admin|webadmin|sysadmin';
$word = 'admin';
if (preg_match('#^(' . $pattern . ')$#i', $word)) {
echo "<p>$word is not OK</p>";
} else {
echo "<p>$word is OK</p>";
}
$word = 'admin2';
if (preg_match('#^(' . $pattern . ')$#i', $word)) {
echo "<p>$word is not OK</p>";
} else {
echo "<p>$word is OK</p>";
}
Parentheses are probably not necessary, but I like using them, to isolate what I wanted.
And, you'll get the same kind of output :
admin is not OK
admin2 is OK
You probably don't want to use [ and ] : they mean "any character that is between us", and not "the whole string that is between us".
And, as the reference : manual of the preg syntax ;-)
So, the forbidden words can be part of their username but not the whole thing?
In .NET, the pattern would be:
Allowed = Not RegEx.Match("admin", "^(webmaster|admin|webadmin|sysadmin)$")
The "^" matches the beginning of the string, the "$" matches the end, so it's looking for an exact match on one of those words. I'm a bit fuzzy on the corresponding PHP syntax.