<?php
$str = "asd,ad";
if(preg_match(",",$str)) {
echo "ok";
}
?
It outputs me
No ending delimiter ',' found in....
?>
your pattern can be replaced to strpos instead
if(strpos($str, ",")!==false)
{
echo "ok";
}
You are missing delimitters, try this:
$str = "asd,ad";
if(preg_match("/,/",$str)) {
echo "ok";
}
To find out more instances, you may want to use preg_match_all function too.
Related
I am trying to check if a number say 1 is present in a string say 11,12,13 To do this I am using strpos:
<?
$s = "2,3,11,12,13";
$ss = "1";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
Here I am expecting the code to give fail as output but it gives success.
Is there any function I can use to exactly match the number in a string. I want the number 1 in the string, not the numbers containing 1 like 11,123,100 etc.
Edit 1: It works fine for #Twinfriends answer but the problem is doing it with multiple numbers.
<?
$s = "2,3,11,12,13";
$ss = "1,2";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
This code gives output fail but it should give true.
Edit 2: Check answer https://stackoverflow.com/a/48297002/8490014
The problem has been solved but because of for each loop the output is getting repeated as many times as there are values in the array. How can we get the output repeating the output?
You could do it like this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$toTest = explode(",", $s);
if(in_array("$ss", $toTest)) {
echo "success";
}
else {
echo "fail";
}
?>
If you've any question considering this code, feel free to ask. (Code tested, works well)
The problem has been solved by adding a foreach loop:
<?
$s = "2,3,11,12,13";
$ss = "1,2,3,5";
$catid = explode(",", $s);
$ocat = explode(",",$ss);
foreach($ocat as $abc) {
if(in_array($abc, $catid)) {
echo "success";
}
else {
echo "fail";
}
}
?>
Try this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$haystack = explode(",",$s);
$matches = preg_grep ("/$ss/", $haystack);
echo !empty($matches) ? "success" : "fail";
You can check by appending comma after the variable to check occurrence at start, appending comma before the variable to check occurrence at last or appending comma before and after the variable to check occurrence at middle.
if((substr($s, 0, (strlen($ss)+1)) === $ss.',')||(substr($s, (strlen($s)-strlen($ss)-1), strlen($s)) === ','.$ss)||(strpos($s, ','.$ss.','))){
echo "success";
}
else{
echo "fail";
}
I want to get the special chars found in string using PHP code, Here is my trial code:
$page_num =">256";
if(preg_match('^[-<>]+$', $page_num))
{
//Here it should returns special chars present in string..
}
check this
$page_num =">256";
if(preg_match_all('/\W/', $page_num,$matches))
{
print_r($matches);
}
What you have missed is ending delimiter
$page_num =">256";
if(preg_match('^[-<>]+$^', $page_num))
^// this one
{
//Here it should returns special chars present in string..
}
Demo of your code, here
or you can try this,
$string = 'your string here';
if (preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-]/', $string))
{
echo "yes";
} else {
echo "no";
}
One more solution is
preg_match('![^a-z0-9]!i', $string);
I tried to remove the first letter if it is 'r' for example by using ltrim function, but it didn't work.
How can check if the first letter if the word 'r' or 'n' etc, by using "if"
<?php
$string = "no";
if (ltrim($string ,'r')) {
echo 'Yes';
}
?>
Here is a one liner, everyone else used substring, here is something different:
preg_replace('/(^[Rr])/', '', $string);
Edit
The above will replace it, misunderstood the question. Here you can do the if:
if(preg_match('/(^[Rr])/', $string)){
echo "Yes";
}
String elements can be accessed like it's an array ...
if(strtolower($string[0]) == 'r'){
$string = substr($string, 1);
}
if($str[0] == 'r') {
$str = substr($str, 1);
}
One possible solution:
if(strtolower(substr($string, 0, 1)) === 'r')
{
//do something
}
to remove the first letter:
$str = substr($string, 1);
ltrim is used to remove whitespaces or other predefined character from the left side of a string. You can check string first character using this :
$string = "no";
if($string[0] == 'r'){
echo "yes";
}
See DEMO
Try that:
<?php
$string = "no";
if(substr($string ,0,1) == 'n'){
echo 'Yes';
}
?>
I am trying to search through a URL for a matching string, but the below code snippet doesn't seem to work.
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file = file($url);
if (in_array($search,$file)) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
If you are just searching for an occurrence of a string on the page, you can use
$str = file_get_contents($url);
if (strpos($str, $search) !== false) {
echo 'Success!';
} else {
echo 'Fail';
}
in_array() checks if an array member is equal to your needle.
It is improbable many websites will have a line which is equal to a only.
Also, is allow_url_fopen enabled?
That code will only find a line that has the exact $search string (likely including whitespace). If you're parsing HTML, check PHP's DOMDocument classes. Or, you can use a regex to pull what you need.
As #alex says, check is allow_url_fopen is enabled.
Also you can use strpos to search the string:
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file_content = file_get_contents($url);
if (strpos($file_content, $search) !== false) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
Lets say i have:
$string = 'qwe1ASD#';
if(preg_match('/^[a-zA-Z]+$/', $string))
{
echo 'OK';
}
else
{
echo 'BAD';
}
Now, is there simple solution, to find all characters from $string which don't match expression? So in return, in place of "BAD" i want to have ex. "BAD. You can't use following characters: 1#"
Thanks in advance for any simple hints! :)
Thank you Floern, your answer suit best my needs. It have only one "preg" so it's also good for performance. Thank you again.
I implemented it for now as follw:
if(preg_match_all('/[^a-zA-Z0-9]/s', $string, $forbidden))
{
$forbidden = implode('', array_unique($forbidden[0]));
echo 'BAD. Your string contains forbidden characters: '.htmlentities($forbidden).'';
}
$tmpstring=preg_replace('~[A-Za-z]~','',$string);
if(strlen($tmpstring))
//bad chars: $tmpstring
You could use preg_match_all():
if(preg_match_all('/[^a-zA-Z]/s', $string, $matches)){
var_dump($matches); // BAD
}
else{
echo 'OK';
}
$string = 'qwe1ASD#';
if(preg_match('/^[a-zA-Z]+$/', $string))
{
echo 'OK';
}
else
{
echo 'BAD. You cannot use the following characters: ' + preg_replace('/[a-zA-Z]/', '', $string);
}
There are different ways. I find this one nice:
$check = preg_prelace('/[a-zA-Z]/', '', $string);
if ($check) echo 'BAD ' . $check;
UPDATE:
if (strlen($check)) echo 'BAD ' . $check;