Array elements in String PHP: How can I print_r the matching elements in the order of the string?
Get the solution ↓↓↓I'm trying to identify severalArray
elements present in a string. I'd like to print_r the matching elements as an array in the order of the string.
Example and trial :
$string = "hello my dear, how are you doing today ?";
$array = array('are', 'today','hello','bonjour','HolГ ', 'Wassup');
$result = array('hello','are','today');
function match($array , $string ){
foreach ($array as $a) {
if (strpos($string ,$a) == true) {
return $a;
}
}
}
The function returns the following error :
strpos(): needle is not a string or an integer
Any idea ??
Thanks a lot from France !
Answer
Solution:
The main issue with your code is that youreturn
as soon as you find one match, rather than building an array of all the matching strings. In terms of returning the strings in the order they occur in the string, you can use the return value fromstrpos
as an array key, and thenksort
the result array before returning it:
$string = "hello my dear, how are you doing today ?";
$array = array('are', 'today','hello','bonjour','HolГ ', 'Wassup');
function match($array , $string ) {
$result = [];
foreach ($array as $a) {
if (($pos = strpos($string, $a)) !== false) {
$result[$pos] = $a;
}
}
ksort($result);
return array_values($result);
}
print_r(match($array, $string));
Output:
Array
(
[0] => hello
[1] => are
[2] => today
)
Note that there is an issue with usingstrpos
in that it doesn't consider whether a match is a complete word or not. So for example,match(['are'], 'hare')
will return['are']
. You can work around this by using in place of
strpos
, and surrounding your search string with\b
(a word boundary):
function match($array , $string ) {
$result = [];
foreach ($array as $a) {
if (preg_match("~\b$a\b~", $string, $matches, PREG_OFFSET_CAPTURE)) {
$result[$matches[0][1]] = $a;
}
}
ksort($result);
return array_values($result);
}
Sample usage:
print_r(match($array, $string));
print_r(match(['are'], 'hare'));
Output:
Array
(
[0] => hello
[1] => are
[2] => today
)
Array
(
)
You can further simplify that by using and combining all the elements of
$array
into a regex alternation:
function match($array , $string ) {
$result = [];
foreach ($array as $a) {
if (preg_match_all('~\b' . implode('\b|\b', $array) . '\b~', $string, $matches)) {
$result = $matches[0];
}
}
return $result;
}
Answer
Solution:
I can advice next approach:
explode string to array of words
compare 2 arrays in loop"
<?php $string = "hello my dear, how are you doing today ?"; $array = array('are', 'today','hello','bonjour','HolГ ', 'Wassup'); function match($array , $string ) { $result = []; $string_to_arr = explode(' ', $string); foreach ($string_to_arr as $s) { if (in_array($s, $array)) { array_push($result, $s); } } return $result; } print_r(match($array , $string ));
Answer
Solution:
You can split your original string into a word array, and then intersect that with your word list.
<?php
$string = "hello my dear, how are you doing today ?";
$array = array('are', 'today','hello','bonjour','HolГ ', 'Wassup');
$result = array('hello','are','today');
$matched =
array_values(
array_intersect(
str_word_count($string, 1),
$array
)
);
var_dump($result === $matched);
var_export($matched);
Output:
bool(true)
array (
0 => 'hello',
1 => 'are',
2 => 'today',
)
You could swap thestr_word_count($string, 1)
forpreg_split('/\b/', $string)
.
However the above is case sensitive.
You could swap outarray_intersect
forarray_uintersect
with a case compare.
array_uintersect(str_word_count($string, 1), $array, 'strcasecmp')
For multibyte case comparisons you are probably better off with thepreg_match
route with the appropriate flags or another comparison function.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: closed without sending a request; it was probably just an unused speculative preconnection
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.