I'm trying to display the apostrophe 's after the full name for example Samuel L. Jackson’s but if the last name or middle name is left out the 's is prefixed by a space, for example: Samuel ’s. Can some one help me correct this problem?
Thanks
Here is the PHP code.
if(!empty($first_name) || !empty($middle_name) || !empty($last_name)) {
echo = $first_name . ' ' . $middle_name . ' ' . $last_name . ' \'s';
}
$text = array();
if(!empty($first_name)) {
$text[] = $first_name;
}
if(!empty($middle_name)) {
$text[] = $middle_name;
}
if(!empty($last_name)) {
$text[] = $last_name;
}
if(count($text) > 0) {
echo implode(' ', $text).'\'s';
}
echo trim($first_name . ' ' . $middle_name . ' ' . $last_name). ' \'s';
should do the trick?
One more issue: If you have a first and last name, there will be two spaces in between... is that going to be a problem at some point?
echo trim($first_name . ' ' . $middle_name . ' ' . $last_name) . '\s';
trim will get rid of any trailing spaces.
$a = ""
if (!empty($first_name)
$a .= $first_name . " "
if (!empty($middle_name)
$a .= $middle_name . " "
if (!empty($last_name)
$a .= $last_name . " 's"
This should do the trick.
Why are you using Single Quote ?
You could simply use "'s" no need of escaping.
echo $first_name.(empty($middle_name) ? '' : $middle_name.' ').$last_name."'s"
alternative
$names = array($first_name);
if(!empty($middle_name))
$names[] = $middle_name;
$names[] = $last_name;
echo implode(' ', $names)."'s";
echo
(empty($first_name) ? '' : $first_name) .
(empty($middle_name) ? '' : ' ' . $middle_name) .
(empty($last_name) ? '' : ' ' . $last_name) . "'s";
If the variables will always be set:
echo str_replace(
' ',
' ',
$first_name . ' ' .
$middle_name . ' ' .
$last_name . "'s");
To display a proper single quote in HTML, replace the ' with ’, which displays as ’.
may be
echo htmlspecialchars($full_name, ENT_QUOTES);
will solve the problem
$full_name = trim($first_name.' '.$middle_name);
if(!empty($full_name) && !empty($last_name)){
$full_name .=' '.$last_name."'s";
echo $full_name;
}
Related
I am trying to compare two strings. When compared they are unequal to the computer. But to the human eye the strings appear to be the same.
When I run a test to check the bin2hex with php they are clearly unequal. Some how a set of double quotes is be read as html. Here are some examples:
$string1 = strlen(html_entity_decode($_SESSION['pageTitleArray'][$b]));
$string2 = strlen(html_entity_decode($value));
echo 'Checking: ' . $_SESSION['pageTitleArray'][$b] . " " . bin2hex($_SESSION['pageTitleArray'][$b]);
echo '<br>';
echo 'testing ' . $string1 . " = "bin2hex($string1) . " " . bin2hex($string2);
echo '<br>';
echo 'With: ' . $value . " " . bin2hex($value);
The code above will out put the following information.
Checking: 1 PAIR OF BBC HEAD GASKET GASKETS MULTI LAYERED STEEL 4.585"
312050414952204f46204242432048454144204741534b4554204741534b455453204d554c5449204c41594552454420535445454c20342e35383522
testing 3630 3632 With: 1 PAIR OF BBC HEAD GASKET GASKETS MULTI
LAYERED STEEL 4.585″
312050414952204f46204242432048454144204741534b4554204741534b455453204d554c5449204c41594552454420535445454c20342e3538352623383234333b
false
I am kinda lost on what to do... Any help would be greatly appreciated. Below is the rest of the code so you can get a total feel for what I'm trying to accomplish.
for($b = 0; $b < count($_SESSION['pageTitleArray']); $b++)
{
foreach($_SESSION['pushIdArrayQuery'] as $key => $value)
{
$string1 = strlen(html_entity_decode($_SESSION['pageTitleArray'][$b]));
$string2 = strlen(html_entity_decode($value));
echo 'Checking: ' . $_SESSION['pageTitleArray'][$b] . " " . bin2hex($_SESSION['pageTitleArray'][$b]);
echo '<br>';
echo 'testing ' . $string1 . " = "bin2hex($string1) . " " . bin2hex($string2);
echo '<br>';
echo 'With: ' . $value . " " . bin2hex($value);
if(trim($_SESSION['pageTitleArray'][$b]) == trim($value))
{
echo '<br>';
echo '<h1>Success</h1>';
echo '<br>';
echo '<b>Key: </b>' . $key;
echo '<br>';
echo 'Page Id: ' . $_SESSION['pushTitleArrayQuery'][$key];
echo '<br> ';
}
else {
echo '<br>';
echo 'false';
echo '<br>';
}
}
}
You have two different types of quotations mark (" on the first string and ″ on the second one).
To human eyes they seems similar but they are complete different characters to PHP.
This is how I fixed the problem. Notice, I removed any html attributes with the html_entity_decode() function. Then, from there I was able to strip away all other special characters by using preg_replace() and compare the results.
//Loop through all the page titles from the directories
for($b = 0; $b < count($_SESSION['pageTitleArray']); $b++)
{
//Loop through the page titles gathered from the database
foreach($_SESSION['pushTitleArrayQuery'] as $key => $value)
{
//use html decode to remove the quotes with ENT_NOQUOTES
$removeHtml = html_entity_decode($value, ENT_NOQUOTES);
//Test to make sure the encoding are the same for both variables.
$detectEncoding = mb_detect_encoding($value, "auto");
//remove all non alphanumeric variables in the first string.
$string1 = preg_replace("/[^a-zA-Z0-9]/", "", $_SESSION['pageTitleArray'][$b]);
//remove all non alphanumeric variables in the second string.
$string2 = preg_replace("/[^a-zA-Z0-9]/", "", $removeHtml);
//Print out to see what the results look like from decode and encoding functions
echo 'Testing the removal of html quotes: ' . $removeHtml . ' Encoding: ' . $detectEncoding;
echo '<br>';
//Show what variables are being compaired.
echo 'Checking: ' . $string1 . " " . bin2hex($_SESSION['pageTitleArray'][$b]);
echo '<br>';
echo 'With: ' . $string2 . " " . bin2hex($value);
//If statement to check if the to strings match.
if(trim($string1) === trim($string2))
{
echo '<br>';
echo '<h1>Success</h1>';
echo '<br>';
echo '<b>Key: </b>' . $key;
echo '<br>';
echo 'Page Title: ' . $_SESSION['pushTitleArrayQuery'][$key];
echo '<br>';
echo 'Page Id: ' . $_SESSION['pushIdArrayQuery'][$key];
echo '<br> ';
}
else {
echo '<br>';
echo 'false';
echo '<br>';
}
echo '<br>';
}
}
I recenlythave tried to convert the preg_replace() line to preg_replace_callback, but with no success. I did try the methods on Stackoverflow, but they seem to be different.
Hope I could get some help with it.
function ame_process_bbcode(&$parser, &$param1, $param2 = '')
{
if (class_exists('vB_BbCodeParser_Wysiwyg') AND is_a($parser, 'vB_BbCodeParser_Wysiwyg'))
{
return $text;
}
else
{
global $vbulletin;
($hook = vBulletinHook::fetch_hook('automediaembed_parse_bbcode_start')) ? eval($hook) : false;
$ameinfo = fetch_full_ameinfo();
$text = preg_replace($ameinfo['find'], $ameinfo['replace'], ($param2 ? $param2 : $param1), 1);
($hook = vBulletinHook::fetch_hook('automediaembed_parse_bbcode_end')) ? eval($hook) : false;
return $text;
}
}
Updates: Thanks to #Barmar, I know now that the issue is related to the fetch_full_ameinfo function.. I will add function below. Maybe it will help others in the long run. I will also include the fix whenever I am done. Thanks to #Barmar for the help.
function &fetch_full_ameinfo($findonly = false, $refresh = false)
{
global $db, $vbulletin, $vbphrase, $stylevar;
static $ameinfo = array();
static $inied, $lastfind;
if ($refresh)
{
$inied = false;
}
if ($lastfind && !$findonly)
{
$inied = false;
$ameinfo = array();
}
if (!$inied)
{
if (!$refresh AND $vbulletin->options['automediaembed_cache'])
{
$path = $vbulletin->options['automediaembed_cache_path'];
if (file_exists($path . "findonly.php"));
{
if ($findonly)
{
include($path . "findonly.php");
}
else
{
include($path . "ameinfo.php");
}
$inied = true;
$lastfind = $findonly;
return $ameinfo;
}
}
if ($vbulletin->options['automediaembed_resolve'])
{
$embed = ",IF(extraction=1 AND embedregexp!= '', embedregexp, '') as embedregexp, IF(extraction=1 AND validation!= '', validation, '') as validation";
$embedwhere = " AND ((extraction = 0 AND embedregexp = '') OR (extraction = 1)) ";
}
else
{
$embedwhere = " AND embedregexp = ''";
}
$sql = "SELECT findcode" . (!$findonly ? ", replacecode,title,container,ameid" : ",extraction$embed") . " FROM " . TABLE_PREFIX . "automediaembed WHERE status=1 $embedwhere
ORDER BY displayorder, title ASC";
$results = $db->query_read_slave($sql);
while ($result = $db->fetch_array($results))
{
if ($result['findcode'])
{
if (!$findonly)
{
$ameinfo['find'][] = "~($result[findcode])~ie";
$ameinfo['replace'][] = 'ame_match_bbcode($param1, $param2, \'' . $result['ameid'] . '\', \'' . ame_slasher($result['title']) . '\', ' . $result['container'] . ', \'' . ame_slasher($result['replacecode']) . '\', \'\\1\', \'\\2\', \'\\3\', \'\\4\', \'\\5\', \'\\6\')';
}
else
{
$ameinfo['find'][] = "~(\[url\]$result[findcode]\[/url\])~ie";
$ameinfo['find'][] = "~(\[url=\"?$result[findcode]\"?\](.*?)\[/url\])~ie";
$ameinfo['replace'][] = 'ame_match("\1", "", ' . intval($result['extraction']) .', "' . ($result['embedregexp'] ? "~" . ame_slasher($result['embedregexp']) . "~sim" : "") . '", "' . ($result['validation'] ? "~" . ame_slasher($result['validation']) . "~sim" : "") . '",$ameinfo)';
$ameinfo['replace'][] = 'ame_match("\1", "\2", ' . intval($result['extraction']) .', "' . ($result['embedregexp'] ? "~" . ame_slasher($result['embedregexp']) . "~sim" : "") . '", "' . ($result['validation'] ? "~" . ame_slasher($result['validation']) . "~sim" : "") . '", $ameinfo)';
}
}
}
$inied = true;
}
$lastfind = $findonly;
return $ameinfo;
}
You can't put the replacement function in fetch_full_ameinfo(), because it needs to refer to the $param1 and $param2 variables, which are local to this function.
This means it needs to use eval() in the current function (this is essentially what preg_replace() does internally when it processes the /e flag).
You need to change the replacement string that fetch_full_ameinfo() creates so that it uses a variable instead of \1, \2, etc. to refer to the capture groups, because the callback function receives the captured matches as an array. So replace the block beginning with if (!$findonly) with this:
if (!$findonly)
{
$ameinfo['find'][] = "~($result[findcode])~i";
$ameinfo['replace'][] = 'ame_match_bbcode($param1, $param2, \'' . $result['ameid'] . '\', \'' . ame_slasher($result['title']) . '\', ' . $result['container'] . ', \'' . ame_slasher($result['replacecode']) . '\', \'$match[1]\', \'$match[2]\', \'$match[3]\', \'$match[4]\', \'$match[5]\', \'$match[6]\')';
}
else
{
$ameinfo['find'][] = "~(\[url\]$result[findcode]\[/url\])~i";
$ameinfo['find'][] = "~(\[url=\"?$result[findcode]\"?\](.*?)\[/url\])~i";
$ameinfo['replace'][] = 'ame_match("$match[1]", "", ' . intval($result['extraction']) .', "' . ($result['embedregexp'] ? "~" . ame_slasher($result['embedregexp']) . "~sim" : "") . '", "' . ($result['validation'] ? "~" . ame_slasher($result['validation']) . "~sim" : "") . '",$ameinfo)';
$ameinfo['replace'][] = 'ame_match("$match[1]", "$match[2]", ' . intval($result['extraction']) .', "' . ($result['embedregexp'] ? "~" . ame_slasher($result['embedregexp']) . "~sim" : "") . '", "' . ($result['validation'] ? "~" . ame_slasher($result['validation']) . "~sim" : "") . '", $ameinfo)';
}
Then change your code to:
$text = preg_replace_callback($ameinfo['find'], function($match) use (&$param1, &$param2, &$ameinfo) {
return eval($ameinfo['replace']);
}, ($param2 ? $param2 : $param1), 1);
I have an interactive map application I am building and the database is relatively small at about 11mb. However it has a lot of rows of data the require me to go through sort them into counties and years.
My page load speeds are about 10-11 seconds trying to go through all of this data and get it organized so that I can then load it with a javascript library called mapael.
I need some help trying to get the page load speeds faster if there is any way to do that. The query doesn't really seem to be the problem. It's more the going through the data that I believe is causing the problem.
<?php
$mydb = new wpdb('uofimap_data','ob2UoV2X5tNz','uofimap_data','localhost');
$data = $mydb->get_results('
SELECT state,county,ship_date,total_cost, quantity FROM data
ORDER BY county ASC, ship_date ASC
');
$dataArray = array(
2006 => '',
2007 => '',
2008 => '',
2009 => '',
2010 => '',
2011 => '',
2012 => '',
2013 => '',
2014 => '',
);
$prevCounty = null;
$prevState = null;
$prevYear = null;
$countySum = null;
$quantitySum = null;
foreach ($data as $obj) :
//temp array to push everything into dataArray for year structure and keep all data in tact.
$tempArray = array();
//all the information to use for use
$date = date('Y', strtotime($obj->ship_date));
$state = $obj->state;
$county = $obj->county;
$cost = $obj->total_cost;
$quantity = $obj->quantity;
//Print only the needed values which is the sums of that county for that particular year.
if($prevCounty == $county){
if($prevYear == $date){
} else {
array_push($tempArray, $prevCounty, $prevState, $countySum, $quantitySum);
$dataArray[$prevYear][] = $tempArray;
}
} else {
if($prevCounty == null){
//Only needed if first value in database is a solo county / year.
//echo $county . ', ' . $state . ' : ' . $cost . ' ' . $date . ' New Null<br>';
} else {
array_push($tempArray, $prevCounty, $prevState, $countySum, $quantitySum);
$dataArray[$prevYear][] = $tempArray;
}
}
//Set everything
if($prevCounty == $county){
if($prevYear == $date){
$countySum += $cost;
$quantitySum += $quantity;
//echo $county . ' ' . $countySum . ' '. $state . ' ' . $date . ' ' . $quantitySum . ' Previous Year<br>';
} else {
$countySum = $cost;
$quantitySum += $quantity;
//echo $county . ' ' . $countySum . ' '. $state . ' ' . $date . ' ' . $quantitySum . ' Current Year<br>';
}
} else {
$countySum = $cost;
$quantitySum = $quantity;
//echo $county . ' ' . $countySum . ' '. $state . ' ' . $date . ' ' . $quantitySum . ' New County <br>';
}
$prevCounty = $county;
$prevState = $state;
$prevYear = $date;
endforeach;
$mapaelJson = '<script type="text/javascript">';
$mapaelJson .= 'data={';
foreach($dataArray as $key => $value){
$mapaelJson .= '"' . $key . '": {';
$mapaelJson .= '"areas":{';
foreach($value as $key1 => $value1){
$county = $value1[0];
$county = ucwords(strtolower($county));
$state = $value1[1];
$total = $value1[2];
$quantity = $value[3];
//var_dump($value1);
$mapaelJson .= '"'.$county . ', '. $state . '" : {';
$mapaelJson .= '"county" : "' . $county . '",';
$mapaelJson .= '"value" : "' . $total . '",';
$mapaelJson .= '"quantity" : "' . $quantity . '",';
$mapaelJson .= '},';
}
$mapaelJson .= '} },';
}
$mapaelJson .= '};</script>';
echo $mapaelJson;
?>
Have you looked into profilers? If you could create traces using the free version of Blackfire.io or cachegrind files using xdebug that would give you the best ideas to optimise.
Anyhow, for the time being you definitely need to reduce the number of loops and inner loops. Also, as much as you can, try having less indentations (keep the number of level low for more readability). You can use continue quite often in loops, it generally helps.
So, here is my code and I would like to understand how to get the array out.
function testArray($name, $ages){
$name;
$ages = array();
echo $name . $ages[0] . '<br>' . $ages[1];
}
testArray("michael", [29,45]);
I would like 'ages' to be an array. Is this the correct way to do it?
Pass the array as array:
<?PHP
function testArray($name, $ages){
//$name;
//$ages;
echo $name . $ages[0] . '<br>' . $ages[1];
}
testArray("michael", array(29,45));
?>
The problem with what you are doing is that you are rewriting your array. Instead of
function testArray($name, $ages){
$name;
$ages = array();
echo $name . $ages[0] . '<br>' . $ages[1];
}
testArray("michael", [29,45]);
use
function testArray($name, $ages){
$name;
echo $name . '<br />' . $ages[0] . '<br />' . $ages[1];
}
testArray("michael", [29,45]);
and if you get a parse error, use this:
function testArray($name, $ages){
$name;
echo $name . '<br />' . $ages[0] . '<br />' . $ages[1];
}
testArray("michael", array(29,45));
i need help in correcting this string.
I am trying:
$returnStr = 'Condition<select name="lstCondition" onchange="javascript:addDateTextbox(this.value,' . ' " ' . $colName . ' ", ' . $key . ')">';
I want this:
<select name="lstCondition" onchange="javascript:addDateTextbox(this.value, 'dateTime', 42)>
I am getting this:
<select name="lstCondition" onchange="javascript:addDateTextbox(this.value, " dateTime ", 42)>
In your statement
name="lstCondition" onchange="javascript:addDateTextbox(this.value,' . ' " ' . $colName . ' ", ' . $key . ')">';
you have spaces each side of the ' marks hence you're getting the spaces round the colname.
If you change it to
name="lstCondition" onchange="javascript:addDateTextbox(this.value,' . '\'' . $colName . '\', ' . $key . ')">';
you should get the result you wanted. I have escaped the '
$returnStr = 'Condition<select name="lstCondition"
onchange="javascript:addDateTextbox
(this.value,' . ' \'' . $colName . ' \',' . $key . ')">';
try the below code
<?php
$returnStr = '<select name="lstCondition" onchange="javascript:addDateTextbox(this.value, \'dateTime\', 42)"><option>Select</option></select>';
echo $returnStr;
?>