This question already has answers here:
filter values from an array similar to SQL LIKE '%search%' using PHP
(4 answers)
Closed last month.
I have a search text field, and an array I want to output below.
This is my array:
array=["abc","abcde","ab","abcdef"];
When I enter "ab" in the text field, then the list should appear. "ab" should come first.
ab,
abc,
abcde,
abcdef,
If I type "abc" then list should show:
abc,
abcde,
abcdef,
This should solve your little issue
<?php
$input = preg_quote('cde', '~'); // don't forget to quote input string!
$array=["abc","abcde","ab","abcdef"];
$result = preg_grep('~' . $input . '~', $array);
foreach ($result as $val) {
echo "$val\n";
}
?>
Check this link for more
<?php
$array = ["abc","abcdeab","ab","abcdef"];
$arr = preg_grep('/cde/', $array);
sort($arr);
var_dump($arr);
?>
check this out
Related
I have array values if there are two values i need to add a string "AND" if single value "AND" string should not be added. i have tried with the following code. cant get the required output
$unserialize_meta = array(0=>"Alcor",1=>"President",2=>"Treasurer");
$checks = array();
foreach($unserialize_meta as $meta){
$checks[]= $meta;
}
echo implode(" And ",$checks);
Output:
Alcor And President
Alcor And President And
required output:
Alcor And President
Alcor And President
You can use the implode function for this. Details can be found here.
Considering the above code:
$unserialize_meta = array(0=>"Alcor",1=>"President",2=>"Treasurer");
$checks = implode(" AND ", array_filter($unserialize_meta));
var_dump($checks);
The array_filter will remove any empty values in the array.
I thing you don't need to loop array. you just need to implode array with the string. Please try below code it will add string AND with your array values.
$unserialize_meta = array(0=>"Alcor",1=>"President",2=>"Treasurer");
if(!empty($unserialize_meta )) {
echo implode(" And ",$unserialize_meta);
}
Output:
Alcor And President And Treasurer
There is mistake saved in array values in array I'm getting empty array. So I add a empty array check :
if(!empty($meta)){
$checks[]= $meta;
}
This question already has answers here:
PHP string replace match whole word
(4 answers)
Closed 4 years ago.
I'm trying to replace whole words in a string using str_replace, however, even letters matched in words are being replaced. i tried preg_replace, but couldn't get it to work as i have a big list of words to replace. Any help is appreciated.
$array2 = array('is','of','to','page');
$text = "homepage-of-iso-image";
echo $text1 = str_replace($array2,"",$text);
the output is : home--o-image
For the whole-words issue, there is a solution here using preg_replace(), and your the solution would be to add /\b to the beginning and \b/u to the end of your array-values. The case-insensitive could be handled with preg_replace_callback (see example #1), but if you are working with a small array like your example, I would just recommend duplicating the array-values.
Applied to your example:
$array2 = array(
'/\bis\b/u',
'/\bof\b/u',
'/\bto\b/u',
'/\bpage\b/u',
'/\bIS\b/u',
'/\bOF\b/u',
'/\bTO\b/u',
'/\bPAGE\b/u'
);
$text = "homepage-of-iso-image";
echo $text1 = preg_replace($array2,"",$text);
You could use array_diff
array array_diff ( array $array1 , array $array2 [, array $... ] )
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
<?php
$remove = array('is','of','to','page');
$text = "homepage-of-iso-image";
$parts = explode('-', $text);
$filtered = array_diff($parts, $remove);
print implode('-', $filtered);
Output:
homepage-iso-image
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 1 year ago.
I have values like
"34,37"
and I want to create array using this values, array like
array('34','37')
How to create such array if I have values.
hope this will help you :
you can use explode function if you have values as string;
$string = '34,37';
$data = explode(',',$string):
print_r($data); /*output array*/
for more : http://php.net/manual/en/function.explode.php
If you have a string like this:
$str = "1,2,3,4,5,6";
And you want to convert it into array, just use explode()
$myArray = explode(',', $str);
var_dump($myArray);
Have a look here for more information
As per my comment, you should use preg_split function. for more details about preg_split function please read PHP manual and also you can use explode function Explode function PHP manual
<?php
$string = '34,37';
$keywords = preg_split("/[\s,]+/", $string);
//OR $keywords = preg_split("/,/", $string); separated by comma only
print_r($keywords);
you can check your desired Output here
Try this,
$val = "34,37"
$val = explode(',', $val);
print_r($val);
output of above array:
Array
(
[0] => 34,
[1] => 37
)
$d5 = preg_grep("/(#[0-9]{1,11})/", $d);
in the code above:
$d is a $_POST method textarea input.
I have a preg_grep to find and capture numbers following an # symbol like this: #1234567890
I want to INSERT the $d5 results to a table in the database, but since the preg_grep should return an array I can't put them in as it is. So I tried to use the following method
$d5string = implode(", ", $d5);
but obviously I couldn't address the $d5 results appropriately.
How can I convert this $d5 results to string so I can INSERT the results to a row under related column?
EDIT/UPDATE:
I wrote the below function and realized that I was giving a string to the preg_grep which takes an array. So my question was not logical. I'd like to update my question: How to capture and put regex results to database?
function activeMention($string) {
$find = '/#([0-9]{1,11})/';
return preg_grep($find, $string);
}
I replaced preg_grep() with preg_match_all() in the function. Now the error is gone and var_dump shows int(0)
on variable $d5 = activeMention($string); I put a $_POST['textarea_name'] value as $string
NOW:
The function looks like this:
function activeMention($string) {
$find = '/#([0-9]{1,11})/';
return preg_match_all($find, $string, $matches);
implode(', ', $matches);
}
When I try to insert the variable below to the database I get only the count of captured strings:
$d5 = activeMention($_POST['textarea_name']);
What I actually needed was the array values in 1 string like "#123123, #1234567, #12345"
To get more than one match from a regular expression, you could use preg_match_all like this:
// Define string
$d = 'Here goes one #1234567890 and a #987654321';
// Run regular expression and put matches into $d5 (array with arrays)
$found = preg_match_all("/(#[0-9]{1,11})/", $d, $d5);
// Iterate result
foreach ($d5[0] as $number) {
echo $number, PHP_EOL; // ... or insert into database
}
Output:
#1234567890
#987654321
Here's finally how I did it:
I got rid of the function and used preg_match_all like this:
preg_match_all('/#([0-9]{1,11})/', $_POST['textarea_name'], $out);
$d5s = implode(', ', $out[0]);
and added $d5s to the VALUES in the query to insert into database.
This question already has answers here:
Fastest way of deleting a value in a comma separated list
(4 answers)
Closed 2 years ago.
Let's say I have a string:
cat,mouse,dog,horse
Is there a regex or a function that will work as follows?
1)"cat" return string ->"mouse,dog,horse"
2)"mouse" return string ->"cat,dog,horse"
3)"dog" return string ->"cat,mouse,horse"
4)"horse" return string ->"cat,mouse,dog"
I need to eliminate the selected item from the string and return the remaining parts of the string.
You mean a function that removes a certain element? Try this:
function removeFromString($str, $item) {
$parts = explode(',', $str);
while(($i = array_search($item, $parts)) !== false) {
unset($parts[$i]);
}
return implode(',', $parts);
}
Demo
It's as simple as exploding the string (str_getcsv) and then removing the searched term. If you have an array, then array_diff makes it very simple:
return array_diff(str_getcsv($list), array($search));
Working demo.
This converts both string inputs to arrays, using explode() for the list. Then you just do array_diff() to output what's in the second array but not the first. Finally we implode() everything back into CSV format.
$input = 'cat';
$list = 'cat,mouse,dog,horse';
$array1 = Array($input);
$array2 = explode(',', $list);
$array3 = array_diff($array2, $array1);
$output = implode(',', $array3);