find a word in a sentence in php - php

I have this sentence:
$newalt="My name is Marie";
I want to include a page if marie or josh is found:
$words = array("marie", "josh");
$url_string = explode(" ", $newalt);
if(!in_array($url_string, $words)){
echo "marie or josh are not in array";
}
the problem is, when I run this php it shows "marie or josh are not in array" and marie is the array (or should be).
What is wrong?

The solution using array_intersect and array_map functions:
$newalt = "My name is Marie";
$words = ["marie", "josh"];
$url_string = explode(" ", $newalt);
if (empty(array_intersect($words, array_map("strtolower", $url_string)))) {
echo "marie or josh are not in array";
}
http://php.net/manual/en/function.array-intersect.php

You have 2 errors. First, in_array() is case-sensitive. You need to make the haystack all lowercase first. Second, in_array() does not accept an array as the needle. To overcome this, you can use something like array_diff():
$newalt="My name is Marie";
$words = array("marie", "josh");
$url_string = explode(" ", strtolower($newalt));
if(count(array_diff($words, $url_string) == count($words)){
echo "marie or josh are not in array";
}

The first parameter of the in_array function is a array. Your $words array is filled with strings and not arrays.
Maybe try something like this:
$words = array("marie", "josh");
$urlStrings = explode(" ", $newalt);
foreach ($urlStrings as $urlString) {
if(!in_array($urlString, $words)){
echo "marie or josh are not in array";
}
}

You wrongly used in_array(), in array to check the first parameter is in the second parameter.
in_array(strtolower($words[0]), array_map('strtolower', $url_string)) && in_array(strtolower($words[1]), array_map('strtolower', $url_string))

Related

Split strings with full name into just first name

I know this question has been answered in here before, but the different functions suggested in the other questions have not been to any help for me - and I´ve tried some few.
I have for example this string with three names that outputs from my database every time it's being loaded:
$string = "Joe Hansen, Carl Clarkson Clinton, Miranda Cobweb Fisher-Caine";
I only want it to output:
$string = "Joe, Carl, Miranda";
I tried this one: click - but it only outputs the first name in some situations and not every time. Is there a easy solution to this one? I also tried explode and implode but did not get that to work either.
Something like this?
<?php
$string = "Joe Hansen, Carl Clarkson Clinton, Miranda Cobweb Fisher-Caine";
$names = explode(",", $string);
$firstNames = array_map(function($name) {
$split = explode(" ", trim($name));
return reset($split);
}, $names);
echo implode(", ", $firstNames);
First you can explode() string with comma , separated. Then go through the loop.
and get first name in the array with substr() and then implode with ,.
$string = "Joe Hansen, Carl Clarkson Clinton, Miranda Cobweb Fisher-Caine";
$names = explode(",", $string);
$firstNames = array();
foreach($names as $name){
$firstNames[] = substr(trim($name), 0, strpos(trim($name), " "));
}
echo implode(", ", $firstNames);
First explode the string on ,, loop through the array, then push the elements in a new array and implode it.
$string = "Joe Hansen, Carl Clarkson Clinton, Miranda Cobweb Fisher-Caine";
$new_string=explode(",",$string);
$slipt_arr=explode(" ",$new_string);
$final_arr=array();
foreach ($new_string as $value)
{
$elements=array_filter(explode(" ",$value));
array_push($final_arr,current($elements));
}
echo implode(", ",$final_arr);

What is the best approach to extract a tag

I have this elements where I need to extract this {{search_tag}} and replace by a value
https://www.toto.com/search/10/{{search_tag}}.html#_his_
I tried this but I don't if it's the good way, does'nt work.
$words = explode('{{search_tag}} ',$website_url[$n]);
$exists_at = array_search($seach,$words);
if ($exists_at){
echo "Found at ".$exists_at." key in the \$word array";
}
You can use str_replace
$str = 'https://www.toto.com/search/10/{{search_tag}}.html#_his_';
$val = 'NEW_VALUE';
$new_str = str_replace("{{search_tag}}", $val, $str);
//outputs: https://www.toto.com/search/10/NEW_VALUE.html#_his_

First value comma separated string

There will be a string with a single or multiple with no commas at all.
$str = "a, b";
How do I get the first value before the comma if it contains comma(s)? This is what I did.
if(preg_match('/[,]+/', $str, $f)) {
$firstVal = $f[0];
}
NOTE: Is /^[^,]+/ better suited?
You can use strtok:
$firstVal = strtok($str, ",")
There are several ways to achieve this.
Using substr() and strpos()
echo substr($str,0,strrpos($str,','));
Using explode()
$result = explode(',',$str);
echo $result[0];
Using strstr()
echo strstr($str, ',', true);
Explode, validate and print.
$values = explode(',',$str);
if(key_exists(0,$values) && trim($values[0])!=""){
echo $values[0];
}

How to extract certain text from a string

I've string like this:
hi my best friend #John How Are You?
I want to extract the name from the string:
john
This string has only one name it can be any number of names the string will have. I want to fetch the string between # and [space].
I've tried using explode() and foreach() loop, but I'm not able to get this value.
$text = "hi my best friend #John How Are You?";
preg_match_all("/(#\w+)/", $text, $matches);
var_dump( $matches );
try this
<?php
$string="hi my best friend #John How Are You?";
preg_match('/(?<=hi my best friend #)\S+/i', $string, $match);
echo $match[0];
Output
Jhon
Try this,
$string = "hi my best friend #John How Are You?";
$arr = explode(" ",$string);
foreach($arr as $val){
if (strpos($val,'#') !== false) {
$result = str_replace("#","",$val);
}
}
echo $result;
The regular expression will do fine but if you still want to do this using explode() and loop you may try the following code :-
$str="Hello my name is #John and this is my friend #julia";
$exp=explode(" ",$str);//exploding string from space
foreach($exp as $key=>$val){
if(strpos($val,"#")===false){continue;
}
else{$new[$k]=str_replace("#","",$val);}
}
print_r($new);
Hope this might help you.
Here is a quick working example
$string = "hi my best friend #John How Are You?";
$explodedstring = explode('#', $string);
$explodedforspace = explode(' ',$explodedstring[1]);
echo $explodedforspace[0];
?>

replacing letters with numbers

So i've been trying to get this bit of code to work all day and haven't been able to do it... I wnat to be able to replace letters with a number (or just a value) from an array. this is the code i've got:
$l2n =
array(
'a'=>'1',
'b'=>'2',
'c'=>'3',
'd'=>'4',
'e'=>'5',
'f'=>6,
'g'=>7,
'h'=>8,
'i'=>9,
'j'=>10,
'k'=>11,
'l'=>12,
'm'=>13,
'n'=>14,
'o'=>15,
'p'=>16,
'q'=>17,
'r'=>18,
's'=>19,
't'=>20,
'u'=>21,
'v'=>22,
'w'=>23,
'x'=>24,
'y'=>25,
'z'=>16
);
$string = str_split($string);
$explode = array_shift($string);
if($l2n[$explode] == $explode)
{
echo $l2n[$explode];
}
else
{
echo $l2n['a'];
}
I tried to use Preg_replace but i've never had a good expereince with that function. so If anybody could help me out, hint me in the correct direction, that'd be great.
You can just use str_replace once you've used array_keys and array_values to get each side of the array:
$keys = array_keys($l2n);
$values = array_values($l2n);
$yourstring = 'Hello world!';
echo str_replace($keys, $values, $yourstring);
// H5121215 231518124!
Demo: https://eval.in/77453
Docs:
http://php.net/str_replace
http://php.net/array_keys
http://php.net/array_values
You can simply do:
$string = preg_replace(array_keys($l2n), array_values($l2n), $string);
From the documentation:
If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart.
Why in the world would you use an array for this? Isn't ord() what you are looking for here?
$string = "ABCDE";
foreach ( str_split($string) as $chr ) {
echo ord($chr) - 64; // or 97 if they all are lowercase
echo PHP_EOL;
}

Categories