This question already has answers here:
strpos issue with 0==false?
(3 answers)
Closed 7 years ago.
I am trying to find weather string contain particular word by using php strpos function and i have tried below code
echo strpos('sfsdf/abc/this', 'sfsdf/');
though it should return true because sfsdf/ present in string I am getting 0(false).
here is live example http://codepad.viper-7.com/dqvNCR
The 0 you are getting is not false it is because the targeted value is the first value of your string.
Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.
http://php.net/manual/en/function.strpos.php
Demo:
echo strpos('asdf/sfsdf/abc/this', 'sfsdf/');
Output:
5
because the sfsdf/ starts at the 6th position.
Live demo: http://codepad.viper-7.com/NgQnxL
Use stristr instead.
Returns the matched substring. If needle is not found, returns FALSE.
<?php
if(stristr('sfsdf/abc/this', 'sfsdf/'))
{
echo 1;
}
else
{
echo 0;
}
DEMO
Related
This question already has answers here:
How to check if a string contains certain characters using an array in php
(5 answers)
Closed 12 months ago.
I want to validate that a string consists of numbers or commas or semicolons:
valid: 1.234
valid: 1,234
invalid: 1.23a
invalid: 1.2_4
What works:
use str_split to create an array out of the string and use in_array with array(0,1,2,3,4,5,6,7,8,9,',',';').
But this feels circuitous to me and since I have to do this with millions of strings I want to make sure there is no more efficient way.
Question: Is there a more efficient way?
If you only need to validate a string (not sanitize or modify it), you can use regex. Try the following:
if (preg_match('/^[0-9\,\;\.]+$/', $string)) {
// ok
} else {
// not ok
}
The solution using preg_match function:
// using regexp for (numbers OR commas OR semicolons) - as you've required
function isValid($str) {
return (bool) preg_match("/^([0-9.]|,|;)+$/", $str);
}
var_dump(isValid("1.234")); // bool(true)
var_dump(isValid("1.23a")); // bool(false)
var_dump(isValid("1,234")); // bool(true)
This question already has answers here:
PHP substring extraction. Get the string before the first '/' or the whole string
(14 answers)
Closed 8 months ago.
$A=123132,32132,123132321321,3
$B=1,32,99
$C=456,98,89
$D=1
I want to cut string after first comma
output. . .
$A=123132
$B=1
$C=456
$D=1
$newA = current(explode(",", $A));
from: PHP substring extraction. Get the string before the first '/' or the whole string
You can do this with strpos to get the position of the first comma, and substr to get the value prior to it:
<?php
$val='123132,32132,123132321321,3';
$a=substr($val,0,strpos($val,','));
echo $a;
?>
Output:
123132
There are a number of ways to accomplish this:
substr($A,0,strpos($A,","))
or
current(explode(",", $A))
will both return the value 123132
You can do
$A='123132,32132,123132321321,3';
$t=explode(',',$A);
$result = $t[0];
This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 9 years ago.
Intro
In search, i want to make search for age range.
if a user inserts a '-' included input like 10-30 this way
i will display list of people age greater than 10 and less than 30.
My Question
$q = mysql_real_escape_string(htmlspecialchars("user input from search field"));
From here before entering to make query i want to make two conditions,
One: if there's a '-' in user input or not ? if yes i want to query as according, if not the other way.
So, how can i enter into the first condition ?
This will work:
// use strpos() because strstr() uses more resources
if(strpos("user input from search field", "-") === false)
{
// not found provides a boolean false so you NEED the ===
}
else
{
// found can mean "found at position 0", "found at position 19", etc...
}
strpos()
What NOT to do
if(!strpos("user input from search field", "-"))
The example above will screw you over because strpos() can return a 0 (zero) which is a valid string position just as it is a valid array position.
This is why it is absolutely mandatory to check for === false
Simply use this strstr function :
$string= 'some string with - values ';
if(strstr($string, '-')){
echo 'symbol is isset';
}
This question already has an answer here:
Odd strpos behavior
(1 answer)
Closed 9 years ago.
I want to clean the url addres but first I want to check if what should be replace is in the string. I want to use strpos because is faster, but it fails to detect the string.
$txt = 'http://www.youtube.com/watch?v=aWFmXFFjJfw';
if(strpos($txt, 'http://www.')){
echo 'true strpos'; // not shows
}
if(strstr($txt, 'http://www.')){
echo 'true strstr'; // show
}
Also
$match = strpos($txt, 'http://www.');
var_dump($match); // int(0);
Because it equals 0 which equates to false.
Instead use:
if(strpos($txt, 'http://www.')!==false){
echo 'true strpos'; //shows
}
This question already has answers here:
Filter multidimensional array based on partial match of search value
(3 answers)
Closed 7 years ago.
I have an array of filenames which I need to check against a code, for example
array("120_120_435645.jpg","150_150_312312.jpg","250_250_1232327.jpg");
the string is "312312" so it would match "150_150_312312.jpg" as it contains that string.
If there are no matches at all within the search then flag the code as missing.
I tried in_array but this seems to any return true if it is an exact match, don't know if array_filter will do it wither...
Thanks for any advice...perhaps I have been staring at it too long and a coffee may help :)
$filenames = array("120_120_435645.jpg","150_150_312312.jpg","250_250_1232327.jpg");
$matches = preg_grep("/312312/", $filenames);
print_r($matches);
Output:
Array
(
[1] => 150_150_312312.jpg
)
Or, if you don't want to use regex, you can simply use strpos as suggested in this answer:
foreach ($filenames as $filename) {
if (strpos($filename,'312312') !== false) {
echo 'True';
}
}
Demo!