Am trying to delete a word from a string in my database
The string is $keywords = tech,sms,libya,tax
Suppose i want to remove libya from the string how do i go about this
I think for the clarity you might want to explode the string into an array, remove the unwanted element and then reconstruct the string. Like this:
$keywords = 'tech,sms,libya,tax'; // Set String
$keywords = explode(',', $keywords); // Explode into array
unset($keywords[ array_search('libya', $keywords) ]); // Unset the array element with value of 'libya'
$keywords = implode(',',$keywords); // Reconstruct string
Use MySQL's REPLACE function.
Related
How can i trim with array of string in php. If I have an dynamic array as follows :
$arr = array(' ','<?php','?>',"'",'"');
so how can i use this array in trim() to remove those string, I tried very hard in the code below :
$text = trim(trim(trim(trim(trim($text),'<?php'),'?>'),'"'),"'");
but i can not use this because array is dynamic, it may have more than 1000 values.
It takes a lot of time to turn into a loop even after trying it.So I can do anything as follows
$text = trim($text, array(' ','<?php','?>',"'",'"') );
It's possible to apply trim() function to an array. The question seems to be unclear but you can use array_map(). Unclear because there are other enough possible solutions to replace substrings. To just apply trim() function to an array, use the following code.
$array = array(); //Your array
$trimmed_array = array_map('trim', $array); //Your trimmed array is here
If you also want to fulfill the argument requirement in trim() you can apply a custom anonymous function to array_map() like this:
$array = array(); //Your array
$trimmed_array = array_map(function($item){
return trim($item, 'characters to be stripped');
}, $array); //Your trimmed array is here
Problem with CodeIgniter in a where_in clause for a query.
Let's have a look on the query to describe the problem:
WHERE `table`.`table_row` IN ('7296 ,7314 {and so on});
This is the result of my query in CodeIgniter (expression engine) is
$sql = ee()->db->where_in('table.table_row', $entry_ids);
My array $entry_ids is a string, that I obtained previously in my code:
$entry_ids = $ee->TMPL->fetch_param('e_id');
To work my query should be:
WHERE `table`.`table_row` IN ('7296' ,'7314' {and so on});
I've already try to specify that my array is actually an array, not a string:
$entry_ids[] = $ee->TMPL->fetch_param('e_id');
But actually, nothing changed.
So you have list of ids stored as a string, which you want to query off of as an IN statement.
First split your string into an array:
If your ids are separated by a string:
$id_array = explode(" ", $string_of_ids);
If your ids are separated by commas:
$id_array = explode(",", $string_of_ids);
If your ids are separated by commas and single quotes:
$id_array = explode("','", $string_of_ids);
Then you can pass $id_array into your query:
If your id column is of type int:
$this->db->where_in('table.table_row', $id_array);
If your id column is of type string:
Just add single quotes to your ids:
$id_array = "'" . join("','", $id_array) . "'";
$this->db->where_in('table.table_row', $id_array);
Then again, if your $string_of_ids contain quotes around your ids, you could probably skip a step by just doing:
$id_array = explode(",", $string_of_ids);
That should leave your quotes in place, so you don't have to join again.
Hope this will help you:
Use explode to get your string into an array
$string = '7296 ,7314'; // string of ids
$entry_ids = explode(',', $string);
//print_r($entry_ids);
$this->db->where_in('table.table_row',$entry_ids); //use in where clause
Working demo:https://eval.in/1019052
For more http://php.net/manual/en/function.explode.php
Suppose I have a string:
$str="1,3,6,4,0,5";
Now user inputs 3.
I want that to remove 3 from the above string such that above string should become:
$str_mod="1,6,4,0,5";
Is there any function to do the above?
You can split it up, remove the one you want then whack it back together:
$str = "1,3,6,4,0,5";
$userInput = 3;
$bits = explode(',', $str);
$result = array_diff($bits, array($userInput));
echo implode(',', $result); // 1,6,4,0,5
Bonus: Make $userInput an array at the definition to take multiple values out.
preg_replace('/\d[\D*]/','','1,2,3,4,5,6');
in place of \d just place your digit php
If you don't want to do string manipulations, you can split the string into multiple pieces, remove the ones you don't need, and join the components back:
$numberToDelete = 3;
$arr = explode(',',$string);
while(($idx = array_search($numberToDelete, $components)) !== false) {
unset($components[$idx]);
}
$string = implode(',', $components);
The above code will remove all occurrences of 3, if you want only the first one yo be removed you can replace the while by an if.
I have a string of text:
$string = "This is a comment :) :D";
and an array of keys with values:
$smileys = Array(':)' => 'smile.gif', ':D' => 'happy.gif');
I want to replace any occurrences of array keys in the string with their related value so the output string would be:
$string = "This is a comment smile.gif happy.gif";
How can I do this? I've tried looping as below but no luck?
foreach($smileys as $smiley){
$string = preg_replace("~\b$smileys\b~", $smileys[$smiley], $string);
}
Edit
I also wish to add some html between the array and replace so:
:D
turns into
<img src="/happy.gif" />
but would the same html need to be in every array value if strtr were used?
try
$string= strtr($string,$smileys);
This will walk through $string and replace each occurence of each key in $smileys with the associated value.
Edit:
To include the <img> tags into the string you could post-process the whole string with a single
$string=preg_replace('/([\w]+\.gif)/i','<img src="$1">',$string);
This of course relies on the assumption that all your gif names do not contain any blanks and that there are no other words like image.gif in your string since they would be affected too ...
Try this:
foreach($smileys as $key => $value)
{
str_replace($key,$value,$string);
}
This should do
foreach($smileys as $key=>$value){
$string = str_replace($smiley[$key], $smiley[$value], $string);
}
I have an array which I get from an exploded url (using $_GET).
The elements in the url are separated by commas but when I COUNT the elements the result includes the final comma. Eg: '?list=jonny,sally,bob,' returns '4' when '?list=jonny,sally,bob' returns '3'. I can't avoid the final comma as they are genrated with them automatically but I need to return 3 on both examples. Any ideas please?? Thanks
$list = explode(",", $_GET['list']);
$listCount = count($list);
//$listCount =(int)$listCount -1;
//$list[$listCount]=str_replace($list[$listCount],',','');
echo $listCount;
NB: the commented out lines are a failed attempt to remove the comma. But $list[$listCount] ,ie the final array element doesn't seem to exist even though it is counted
If you want to trim any extra commas at the start or end of the string, use trim(). If you want it at the end of the string, you can use rtrim().
$list = explode(",", $_GET['list']);
to
$list = explode(",", trim($_GET['list'], ','));
Trim any commas first:
$strList = rtrim($_GET['list'], ",")
$arrList = explode(",", $strList);
Array_filter will remove any empty values from your array, so in case you have two commas in a row, it will remove empty values caused by that also.
$list = array_filter(explode(",", $_GET['lists']));