While executing this code from a webservice, the title and keywords are shown in the meta tags as expected. The description stays empty though. How do I put dynamic array content from $array in the description? Ps eg $array[3] won't work either.
$array = $result->AAAResult->AAA->A;
$teller = count($array);
$titeltekst = "{$teller} quantity: $r";
$doc =& JFactory::getDocument();
$options = $doc->getHeadData();
$options['title'] = $titeltekst;
$options['metaTags']['standard']['keywords'] = "keywords - test";
$options['metaTags']['standard']['description'] = $array;
$doc->setHeadData($options);
You wont be able to assign an array directly to anything that expects a string, but you can use implode assuming that all the values are also strings.
$string = implode(', ', $array);
Ok I just added eg.
$string .= $v->Naam;
and;
$options['metaTags']['standard']['description'] = $string;
, if you choose to create the string on the Naam (Name) object of the Array.
Related
I am stuck with a problem, I have fetched some values from a MySQL query and put them in to an array like so:
$add1 = $location->address1;
$add2 = $location->address2;
$twn = $location->town;
$pcode = $location->postcode;
$latitude = $location->lat;
$longitude = $location->lng;
$fullAddress = [$add1, $add2, $twn, $pcode];
$string = rtrim(implode(',', $fullAddress), ',');
echo $string;
so that I can echo out a users address. The problem I am getting is that even if one of these values does not exist (and some don't because they are not all required fields), the comma is still echoed to the screen like:
add1,, town, br2 5lp
because there is an empty value in the database.
What I want to achieve is something like:
add1, town, br2 5lp
if the second part of the address is missing.
Can anyone help me figure this out?
try this.
$fullAddress = [$add1, $add2, $twn, $pcode];
$string = implode(',', array_filter($fullAddress, 'strlen'));
echo $string;
The problem I am getting is that even if one of these values does not
exist (and some dont because they are not all required fields),
the comma is still echoed to the screen
That is what the implode function in php is supposed to do. If you want a different behavior, you will have to either change the way you create your CSV string or do some extra processing on the information you obtain from the implode function.
So use a for loop or a foreach loop to go through the address array. A for loop is faster than a foreach loop.
Using Foreach:
$add1 = $location->address1;
$add2 = $location->address2;
$twn = $location->town;
$pcode = $location->postcode;
$latitude = $location->lat;
$longitude = $location->lng;
$fullAddress = [$add1, $add2, $twn, $pcode];
$string = "";
foreach($fullAddress as $value)
{
if(!empty($value))
$string .= $value.", ";
}
$string = rtrim($string, ", ");
echo $string;
You can do some extra processing on the created csv string of your own solution by maybe doing a str_replace() of all occurances of ,, with ,. This could be dangerous because if any of the values in your $fullAddress array contain a ,, as a valid string then that will also be replaced too with ,.
What you need is to assure array $fullAddress have no empty value, so php built-in function array_filter make this purpose very easy.
Just change your last codes to:
$string = implode(',', array_filter($fullAddress));
echo $string;
If the second parameter of array_filter is not supplied, all entries of array equal to FALSE will be removed. So just simply use array_filter($fullAddress) it make the code more clear and simple.
I have a string like this [tubelist dijfisj, ijdsifjad, ajkdfksd, sdjfkdf] and I would like to separate them into two ###URL### and ###URL2###.
This is the code I got so far
function xyz_plugin_callback($match)
{
$tag_parts = explode(",", rtrim($match[0], "]"));
$output = YOUX_TARGET;
$output = str_replace("###URL###", $tag_parts[1], $output);
$output = str_replace("###URL2###", $tag_parts[2], $output);
}
$match is the variable that I'm passing in.
You can utilize regular expressions here.
So if you do something like:
$temp_match = trim($match[0], "[]");
$urls = array();
preg_match("/([\w\s]*),([\w\s]*),.*/", $temp_match, $urls);
$url1 = $urls[1];
$url2 = $urls[2];
// do your $output str_replace here.
finally, I got it working! Here is the working code.
$tag_parts1 = explode(" ", rtrim($match[0], "]"));
$tag_parts = explode(",",$tag_parts1[1],2);
the first line will strip the [tubelist and ].
the second line with store the first set of value in array[0] and the rest to [1].
voila, case resolved.
I am trying to print the different category's selected in a single line in xml,
like
<cat_name>meeting, food and drinks, sports</cat_name>
The output I am getting:
<cat_name>meeting, food and drinks, sports,</cat_name>
I want to remove only the last comma.
The code I have written so far is:
$sq="
select category_main.cat_name
from category_main
join category
on(category.cat_id=category_main.cat_id)
where category.event_id='$event_id'
";
$e=mysql_query($sq);
$xml ='<cat_name>';
while($row=mysql_fetch_array($e))
{
$res=$row['cat_name'];
//$new = substr($val,0,-1);
$xml .="$res, ";
}
$xml .='</cat_name>';
echo $xml;
Call trim() with the optional second parameter as a ','
trim($cat_name, ',')
However, since you're doing this in a while loop, you should build an array then implode() it rather than building the string in the loop. This avoids the extra comma to begin with.
$arr = array();
while($row=mysql_fetch_array($e))
{
$arr[] = $row['cat_name'];
}
$cat_name = implode(",", $arr);
// $cat_name is now "meeting, food and drinks, sports"
You can do this by [i used my variable]
$str = substr($str,0, (strlen($str)-1));
Or use a rtrim, rtrim( $cat , ',' ) http://php.net/rtrim
hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);
I've gone through this address:
Passing an array to a query using a WHERE clause
and found that if I use a join clause to separate values , is also at the end of the array. How can I remove last?
I am using like this
$ttst=array();
$ttst=array();
$tt = "SELECT frd_id FROM network WHERE mem_id='$userId'";
$appLdone = execute_query($tt, true, "select");
foreach($appLdone as $kk=>$applist){
$ttst[] = $applist['frd_id'];
}
$result = implode(',', $ttst);
then also coming last ,
Thanks.
but it doesn't give single quote to each value .
join(',', array_filter($galleries))
array_filter gets rid of empty elements that you seem to have. Of course, you should take care not to have empty elements in there in the first place.
You could use trim() (or rtrim()):
$myStr = 'planes,trains,automobiles,';
$myStr = trim($myStr, ',');
$str = "orange,banana,apple,";
$str = rtrim($str,',');
This will result in
$str = "orange,banana,apple";
Update
Based on your situation:
$result = implode(',', $ttst);
$result = rtrim($result,',');
$arr = array(...);
$last = end($arr);
unset($last);
If the trailing "," is an array element of itself, use array_pop(), else, use rtrim()
$array = array('one','two',3,',');
array_pop($array);
print_r($array);
Gives:
Array ( [0] => one 1 => two 2 => 3
)
No need for length. Just take from beginning to the (last character - 1)
$finalResult = $str = substr($result, 0, -1);
Use implode() instead! See Example #1 here http://php.net/manual/en/function.implode.php
Edit:
I think a quick fix would be (broken down in steps):
$temp = rtrim(implode(',', $ttst), ','); //trim last comma caused by last empty value
$temp2 = "'"; //starting quote
$temp2 .= str_replace(",", "','", $temp); //quotes around commas
$temp2 .= "'"; //ending quote
$result = $temp2; // = 'apples','bananas','oranges'
This should give you single-quote around the strings, but note that if some more values in the array are sometimes empty you will probably have to write a foreach loop and build the string.