while displaying address from database I have added , at the end of each value. when last value of address in empty am getting , along with the last value. But I don't want that. Can any one help?
if ($Fetch['addr']!=''){
echo $Fetch['addr'].',';
it is displaying
address,city,postalcode
if i remove postalcode it displayes
address,city,
but i don't need , at the end when any of the value is not provided
Insert your value in array and at the end of it use join to convert array to string with:
$array[] = [Your Value Here];
$string = join ( ',' , $array )
Store the output in a variable do not echo directly with if as you are doing and then use below function and finally print them after applying rtrim on the $str.
string should be stored in $str with all the if's
if ($Fetch['addr']!=''){
$str.=$Fetch['addr'].',';
This will remove last comma
echo $clean = rtrim($str,',');
Use Substr() It will really helps!
$str = "abcde,";
$str = substr($str,'',-1);
Now this will remove , from String.
Related
$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.
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.
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']));
How can I flip the order of text
$string = 'last row
something inbetween
first row';
Result should be:
first row
something inbetween
last row
Note this is no A-Z ordering, just flipping it.
Is it possible? A solution in PHP, javascript or jquery would be welcome
You could explode the string into an array, reverse it and put it back together:
$a=explode("\n",$string);
$string=implode("\n",array_reverse($a));
You can split the string and reverse the array:
$array = explode("\n", $string);
$array = array_reverse($array);
$string = implode("\n", $array);
Edit: no one wrote an javascript solution (same logic as php script):
var string = string.split("\n").reverse().join("\n");
Edit: an example: http://jsfiddle.net/ceKyF/
You only got PHP answers, here is a javascript one:
// Split the string into an array
var arr = string.split( '\n' )
// Reverse the array
arr.reverse()
// Join the array back into a string
string = arr.join( '\n' )
This can be done in one line:
string = string.split( '\n' ).reverse().join( '\n' )
I've got a list of data with in the following format:
data\n
data\n
data\n
data\n
data\n
Now I try to explode it into an array with
$array = explode("\n", $dataList);
What happens next is that there is a key with no data, I think it is because of the \n on the end.
Is there a way to explode it so that the last key isn't set?
Thanks!
Not directly. You can either:
Remove the trailing "\n" with trim.
Remove the last element of $array with array_pop.
Use preg_split instead with the flag PREG_SPLIT_NO_EMPTY.
Remove empty values by:
$array = array_filter( $array );
After you explode, use array_pop() to pop the last item:
$array = explode("\n", $dataList);
array_pop($array);
You can add an if statement using count() and empty() if you want to check if the last item contains something other than a linebreak character, but that should get you what you need.