I have an issue that I am stuck in,
I have a table called wpg_original_word with field tags, which contains comma separated values and what I want to achieve is search for tags which are starting with the letter 'd'
I have written the query
SELECT DISTINCT tags FROM wpg_original_word WHERE tags LIKE 'd%' ;
It returns the whole string like 'Diabetes, Nutrition, Dietician' , but I want only the Diabetes, Dietician.
Any help?
You could split the string with PHP, use the following code
<?php
$string = 'Diabetes, Nutrition, Dietician';
$array = explode(',', $string);
$result = array();
foreach ($array as $part) {
if (substr(strtolower(trim($part)),0,1) == 'd') {
array_push($result, trim($part));
}
}
var_dump($result);
See it in action here.
http://codepad.org/SVk7FPx9
You can use in_array() to check for a string in your query and then print it.
Related
In php i would like to put a string like that :
*1\t1\tSomejehjdbsj\t7\t10\t5\t10\t0\t0\t0\t0\t0\t0\t--:--\t0\t0\t0\t0\t0\t00:00:00\t0\t1\t0\t0\t1f7ef741\t15:42\t99\t1026\t1\t--:--\tShowVault\t0\t1f7ef74187664f03876538511f30a5af\tSomejehjdbsj\t0\t0\t00000000000000000000000000000000\t\t00000000000000000000000000000000\t00000000000000000000000000000000\t0\t0\tNC-Series\t1\t12\tSCOPE\t16\t-1\t0\t0\t0\t0\t0\t0\t0\t0\t0\t\t0\t\t\n"]}*
But each \t split my string and its differents values.
And the \n is another "row" of that kind of stuff.
I need those values so i thinks i could put it in an array, But i don't know how to do that.
thanks!
Try something like this:
$testString = 'AAA\tBBB\nCCC\tDDD';
$result = explode('\\t', $testString);
foreach ($result as $key => $current) {
if (strpos($current, '\\n')) {
$result[$key] = explode('\\n', $current);
}
}
var_dump($result);
See it in action here: https://3v4l.org/92ttB
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 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']));
I am trying to do a search query to see if a textarea contains some keywords. I'm having a bit of trouble though plugging my textarea's values into the query.
This is my query:
$match = $this->input->post('wStory');
$this->db->where("`terms` LIKE '%$match%'");
$query = $this->db->get('filter_tbl');
return $query->num_rows();
The $match is my text field and what I have been trying to do is split up the words inside and then run through each individual word. I have tried using PHPs' explode() function which sort of works, but in this case it doesn't work because it turns the string into an array.
Is there any way I can split up the strings in my textarea to just words and run through the words in the like statement, Or is there just something I'm missing?
The query you're currently running is doing a check for a specific phrase, rather than checking for any of the specified words.
You need to do the following:
$match = $this->input->post('wStory');
// break search phrase into keywords
$keywords = explode(' ', $match);
// Build query
foreach ($keywords as $keyword)
{
$keyword = trim($keyword);
$this->db->or_where("`terms` LIKE '%$keyword%'");
}
$query = $this->db->get('filter_tbl');
return $query->num_rows();
The explode(' ', $match) does not take into account any punctuation that might separate words.
Isn't there a $this->db->like("terms", $match); method?
See http://ellislab.com/codeigniter/user-guide/database/active_record.html#like
I think fulltext search is the way to go. The url Rob W posted also gives this:
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
which in you care would be something like
$this->db->where('MATCH (terms) AGAINST ("$match")', NULL, FALSE);
Edit: After reading some further, this might be better (or atleast better readable):
$this->db->where('MATCH (terms) AGAINST ("$match")');
Try this:
$match = $this->input->post('wStory');
//Getting words from textarea and put them into array
$wordsArray = explode(' ',$match);
if (!empty($wordsArray)) {
//use where_in() instead of where()
$this->db->where_in("`terms`", $wordsArray);
$query = $this->db->get('filter_tbl');
return $query->num_rows();
}
I have this query:
list[One]=1&list[Two]=2&list[Apple]=fruit
this is the regex I use to return the values in the brackets and after the equal sign
preg_match_all('/(?<query>list\[(?<pagename>.*?)\]\=(?<parent>.*?))/',$source,$array);
returns:
One=
Two=
Apple=
Values that come after the equal sign are missing. Where's my mistake?
By the way, this query is generated with jquery's serialize(). Is there a better method to parse the values?
As I made in a comment, you may want to look in to parse_str
However, if you change the final .*? to something like [^&]* then you'll probbaly have better luck (assuming this is a GET query string (or some facsimile) as & will have to be escaped from the sequence with %26)
(?<parent>.*?) matches an empty string, so the result ist 'correct'. Try (?<parent>[^&]+) instead:
preg_match_all('/(?<query>list\[(?<pagename>.*?)\]\=(?<parent>[^&]+))/',$source,$array);
Because you use the non-greedy ? for <parent>, it's not grabbing the values. Try the other answers or if you can count on the format list[<name>]=<value> then you can avoid using regex altogether.
$query = 'list[One]=1&list[Two]=2&list[Apple]=fruit';
$pieces = explode('&', $query);
$matches = array();
foreach ($pieces as $piece) {
list($key, $value) = explode('=', $piece);
$matches[substr($key, 5, -1)] = $value;
}