This question already has answers here:
How do you make a string in PHP with a backslash in it? [closed]
(3 answers)
Find the occurrence of backslash in a string
(3 answers)
Closed 3 years ago.
I am trying to see if the string contains \ and if i put it like this
$search4 = '\'; or like $search4 = "\"; it won't work as this is incorrect.
This is my search function
function Search($search, $string){
$position = strpos($string, $search);
if ($position == true){
return 'true';
}
else{
return 'false';
}}
And i am calling it like that echo Search($search4, $string);
You need to escape the \ by using 2 \. Because '\' is escaping the single quote and is giving you an error. The same will happend with double quotes.
http://php.net/manual/en/regexp.reference.escape.php
function Search($search, $string){
$position = strpos($string, $search);
if ($position == true){
return 'true';
}else{
return 'false';
}
}
$search = '\\';
print Search($search, "someString");
Related
This question already has answers here:
Is there a PHP function that can escape regex patterns before they are applied?
(2 answers)
Closed 5 years ago.
I want to check if the current $uri_string matches any of these patterns
$uri_string = 'project/details/1258';
$uri_patterns = [
'project/foo/',
'project/bizz/',
];
foreach($uri_patterns as $pattern) {
if(preg_match('/' . $pattern. '/i', $uri_string) == 1) {
echo 'Is a match!';
}
}
so if at some point the $uri_string = 'project/foo/123/some-other-params'; then this sould match the 'project/foo/' pattern. How can I do this?
You need to escape the / slashes from your regex like:
$uri_string = 'project/details/1258';
$uri_patterns = [
'project/foo/',
'project/bizz/',
];
foreach($uri_patterns as $pattern) {
$pattern = preg_quote($pattern, '/');
if(preg_match("/$pattern/i", $uri_string) == 1) {
echo 'Is a match!';
}
}
This uses preg_quote() to escape the slashes.
Alternatively, you can use different delimiter for the regex, like #:
preg_match("#$pattern#i", $uri_string);
Or you can ignore regex completely and use string parsing functions like:
foreach($uri_patterns as $pattern) {
if(strpos($uri_string, $pattern) !== false) {
echo 'Is a match!';
}
}
I think you'll need to escape those forward slashes:
$uri_patterns = [
'project\/foo\/',
'project\/bizz\/',
];
Then for your match, use:
preg_match('/' . $pattern . '/i', $uri_string, $matches);
if (!empty($matches)) {
echo 'Is a match!';
}
I am trying to check for two conditions,
The string should contain a vowel.
The string should contain a space.
Here is what I am writing:
$reg = "/(?=.*(\s)) (?=.*(a|e|i|o|u))/";
But on running:
if ( preg_match($reg,"kka "))
echo "YES.";
else
echo "NO.";
I am getting NO. What am I doing wrong?
((?:.*)[aeiouAEIOU]+(?:.*)[ ]+(?:.*))|(?:.*)[ ]+((?:.*)[aeiouAEIOU]+(?:.*))
You can try with this
Explnation
Here is the correct way to use lookaheads:
^((?=.*\s.*).)((?=.*[aeiou].*).).*$
Demo here:
Regex101
If you want an option which does not involve using a regex would be to remove spaces/vowels from the input string and verify that the resulting length has decreased.
$input = "kka ";
if (strlen(preg_replace("/\s/", "", $input)) < strlen($input) &&
strlen(preg_replace("/[aeiouAEIOU]/", "", $input)) < strlen($input)) {
echo "both conditions satisfied"
else {
echo "both conditions not satisfied"
}
The alternative solution using preg_replace and strpos functions:
$str = " aa k";
if (($replaced = preg_replace("/[^aeiou ]/i", "", $str)) && strlen($replaced) >= 2
&& strpos($replaced, " ") !== false) {
echo 'Yes';
} else {
echo 'No';
}
I'm trying to preg_match this string.
$word = "$1$s";
or
$word = "$2$s"
if(preg_match('/^\$[1-9]{1}\$s$/' ,$word)){
echo 'true';
} else {
echo 'false';
}
I tried this but it is not giving a true result.
How can I make this true.
PHP is trying to render the variable $s in the double quoted string. Use single quotes instead and you can remove the {1} inside your regular expression because it is not necessary.
$word = '$1$s';
if (preg_match('/^\$[1-9]\$s$/', $word)) {
echo 'true';
} else {
echo 'false';
}
You can also escape the $ in your double quoted string:
$word = "$1\$s";
// Note $1 doesn't need to be escaped since variables can't start with numbers
Finally, you can see why this wasn't working by seeing what your $word actually equaled (with error reporting enabled):
$word = "$1$s"; // Notice: Undefined variable: s on line #
echo $word; // $1
Try this:
if(preg_match('/\$[1-9]{1}\$s/',$word)){
echo 'true';
} else {
echo 'false';
}
This question already has answers here:
Using an array as needles in strpos
(16 answers)
Closed 9 years ago.
Im running strpos on a <a> tag to see if it contains either one of two urls.
At the moment im using this bellow - how would i set it to check if - tumblr.com OR google.com were present ?
function find_excluded_url ($url) {
$find = "tumblr.com"; // OR GOOGLE.COM ....
$pos = strpos($url, $find);
if ($pos === false) {
return false;
} else {
return true;
}
}
// SET URL
$url = "<a href='http://tumblr.com/my_post' rel='nofollow'>This site</a>";
// CALL FUNC
$run_url = find_excluded_url($url);
if ($run_url == true) {
echo "URL - " . $url . "<br>";
}
You can't use two needles in strpos. But what you can do, is use it twice, with an or:
function find_excluded_url ($url) {
return (strpos($url, "tumblr.com")!==false) || (strpos($url, "google.com")!==false);
}
For more complicated searches, you might want to look into Regular Expressions. This would work:
$subject = 'blablabgoogle
balblabtumblrasd
blaasdgoogleadsad';
$pattern = '#(?:google\.com|tumblr\.com)#i';
$result = preg_match($pattern, $subject, $subpattern, PREG_OFFSET_CAPTURE);
if($result) echo 'Position: ' . $subpattern[0][1];
The performance of this (if performance is an issue for you) depends on how many search queries you have and how big your haystack is. Regular expressions come with a relatively big overhead, however, they only have to run over the text once. If you use strpos twice, this gets expensive with long strings. If performance is really an issue, you could also write your own strpos that goes character per character. I doubt, however, that this is necessary.
function find_excluded_url ($url, $searchURL) {
$pos = strpos($url, $searchURL);
if ($pos === false) {
return false;
} else {
return true;
}
}
// SET URL
$url = "<a href='http://tumblr.com/my_post' rel='nofollow'>This site</a>";
// CALL FUNC
$run_url = find_excluded_url($url, 'google.com');
if ($run_url == true)
echo "URL - " . $url . "<br>";
$run_url = find_excluded_url($url, 'tumblr.com');
if ($run_url == true)
echo "URL - " . $url . "<br>";
This question already has answers here:
Make all words lowercase and the first letter of each word uppercase
(3 answers)
Closed 1 year ago.
We have a database of Canadian addresses all in CAPS , the client requested that we transform to lower case expect the first letter and the letter after a '-'
So i made this function , but I'm having problem with french accented letters .
When having the file and charset as ISO-88591 It works fine , but when i try to make it UTF-8 it doesn't work anymore .
Example of input : 'damien-claude élanger'
output : Damien-Claude élanger
the é in utf-8 will become �
function cap_letter($string) {
$lower = str_split("àáâçèéêë");
$caps = str_split("ÀÁÂÇÈÉÊË");
$letters = str_split(strtolower($string));
foreach($letters as $code => $letter) {
if($letter === '-' || $letter === ' ') {
$position = array_search($letters[$code+1],$lower);
if($position !== false) {
// test
echo $letters[$code+1] . ' == ' . $caps[$position] ;
$letters[$code+1] = $caps[$position];
}
else {
$letters[$code+1] = mb_strtoupper($letters[$code+1]);
}
}
}
//return ucwords(implode($letters)) ;
return implode($letters) ;
}
The Other solution i have in mind is to do : ucwords(strtolower($str)) since all the addresses are already in caps so the É will stay É even after applying strtolower .
But then I'll have the problem of having É inside ex : XXXÉXXÉ
Try mb_* string functions for multibyte characters.
echo mb_convert_case(mb_strtolower($str), MB_CASE_TITLE, "UTF-8");
I have the same problem in spanish, and I create this function
function capitalize($string)
{
if (mb_detect_encoding($string) === 'UTF-8') {
$string = mb_convert_case(utf8_encode($string), MB_CASE_TITLE, 'UTF-8');
} else {
$string = mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
}
return $string;
}