Why strpos fail in this example [duplicate] - php

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
}

Related

How to use substr() on php? [duplicate]

This question already has answers here:
How substr function works? [closed]
(3 answers)
Closed 4 years ago.
I am trying to get "pa" from "a(bcdefghijkl(mno)pa)q".
This is my code for exampe:
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,15);
echo $mystring;
outputs is:
mno)pa)q
You have use right code but the second parameter is wrong. use this one below
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,2);
echo $mystring;
In substr function:
first parameter means from which position of string starts.
and the second parameter means how many characters you have want.
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,-4,2);
echo $mystring;
Try this

PHP - If string contains '<' or '>' - strpos() [duplicate]

This question already has answers here:
Detect HTML tags in a string
(8 answers)
Closed 4 years ago.
I want to do a simple kind of test to see if a string contains any HTML.
In this case if the $string variable is test or <test> it returns no for me:
$string = 'test';
if(strpos($string,'<') !== 'false'){
echo 'no';
}else{
echo 'yes';
}
Is there a better way to check if a string contains HTML? I don't want to do anything to the string just check if it has HTML tags?
if($string != strip_tags($string)) {
// contains HTML
}
Took the answer from here

Check if string contains word (not a sentence string) [duplicate]

This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 7 years ago.
I'm writing a PhP script, but for a filter I need to search for a string in a string.
this is the script I have at the moment:
<?php
$stringtocheck = "Schiphol-Almere C./Hilversum/Utrecht C.";
$searchfor = "Hilversum";
// do the magic stuff over here
?>
How do i get the script to search for '$searchfor' in '$stringtocheck'?
Use strpos for to find the needle-string in the haystack-string. And here the haystack-string is stringtocheck and the needle-string is searchfor.
Example of using the function:
<?php
$stringtocheck = "Schiphol-Almere C./Hilversum/Utrecht C.";
$searchfor = "Hilversum";
if(strpos($stringtocheck, $searchfor) !== false)
echo true;
?>

strpos not working with string containig forward salsh [duplicate]

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

How to find if a variable contains certain character or not ? PHP [duplicate]

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';
}

Categories