[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

inserting multiple no break space into list select via xml retrieved from server side

hi there,
i am having some trouble in that it is populating \u00a0 instead of a nobreak space into the list select.
how should i populate a nobreak space from php generated xml into the <header></header> tag so it can be displayed correctly in the web page via the javascript?
xml excerpt:
<header>#110010451\u00a0\u00a0SENT\u00a0\u00a0\u00a0\u00a0\u00a02011-10-29 02:48:53\u00a0\u00a0Pierre du Toit ( pierre@greywacke.co.za )</header>

Open in new window

&nbsp; instead also doesn't work - i need to save the no break space unicode character in the database so they populate. perhaps alt+0160 when saving to the database? 0o
sincerely,
Pierre du Toit.
lead-manager.jpg
0
intellisource
Asked:
intellisource
2 Solutions
 
intellisourceAuthor Commented:
unfortunately this did not work. seems i am having trouble with posting the ajax request encoding this field in the encodeURIComponent function.
once received by the handler, it saves the configurable text to mysql.
this is then retrieved from the database as the following
#%LEADREFERENCE%  %LEADSUCCESS%  %LEADCREATED%  %CONSUMERNAME% ( %CONSUMEREMAIL% )

Open in new window

and the flags are populated.
the %LEADSUCCESS% flag is populated by either "SENT   " or "UNSENT" depending on the lead value with the following php.
			case "LEADSUCCESS":
				if ($GLOBALS["leadsuccess"]) {
					return "SENT   ";
				} else {
					return "UNSENT";
				}
				break;

Open in new window

however from the first screenshot it can clearly be seen that the nobreak spaces do not populate as expected. the second screenshot gives an example of them doing this, where they are manually put in by javascript inbetween the variables retrieved from xml.
the whole issue of this excersize is to make the select list "headers", configurable.
just seems i am going about it the wrong way to populate the "headers" from php generated xml so that the nobreak spaces display properly.
could somebody please assist!
lead-manager-2.jpg
lead-manager-3.jpg
0
 
ukerandiCommented:
use <br> tag to break
0
 
intellisourceAuthor Commented:
and another question - where does the  come from? 0o
the database stored values are:

Open in new window

and the replacement values are:

Open in new window

yet SENT get's 3 Â characters before every nobreak space 0o
somebody please help...
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
intellisourceAuthor Commented:
apologies - forgot to paste the strings above. here they are.
#%LEADREFERENCE%  %LEADSUCCESS%  %LEADCREATED%  %CONSUMERNAME% ( %CONSUMEREMAIL% )

Open in new window

"SENT   "

Open in new window

somehow the php is replacing those nobreak space characters (alt+0160) with " ". how can i get around this? all my php documents are utf-8 and as far as i know so is php's default charset.
0
 
Amar BardoliwalaCommented:
Hello intellisource,

I did not understand your problem exactly, but it seems to have problem with character set.

Did you check it by changing character set of your browser?

can you provide link to your page? if it is online.

can you provide detail code?


is it possible for you to replace space with &nbsp; ?

Thank You.

Amar.
0
 
Ray PaseurCommented:
Please read this article:
http://www.joelonsoftware.com/articles/Unicode.html

PHP has no native support for UTF-8 at all.
http://us.php.net/manual/en/language.types.string.php

What I would do is just put spaces into the data base.  Then if you want to preserve spacing in the rendered XML string, translate the spaces to the &nbsp; entity before displaying the data.
0
 
intellisourceAuthor Commented:
hi amar_bardoliwala,

yes it is as the page specifies - Unicode (UTF-8).

the links are in the screenshots, but unfortunately you will not be able to view them as they are protected on the server (behind a username and password which i am unable to share).

the code which creates the xml document is as follows - i have tried replacing spaces with &nbsp; entity but this the reason why it does not work when being read is quoted after:
// root xml writing code
						echo "			<header>".str_replace(" ","&nbsp;",xmlentities(populateflags("%CONFIGTEXT Intellisource | Premium Lead Manager Header | ".$formname."%")))."</header>\n";
// populateflags & idflags functions from global include
function populateflags($str) {
	// replace flags used in string (parse entire template contents' as string), populating flags individually and sometimes recursively in callback function.
	while (preg_match_all("/%([A-Z]+[^%]*)%/",$str,$arr)) {
		$str = preg_replace_callback("/%([A-Z]+[^%]*)%/","idflags",$str);
	}
	return $str;
}
function idflags($input) {
	global $conn;
	foreach ($input as $in) {
		// use to explode by first space if found, to get flag values
		$in = preg_split("/ /",$in,2,PREG_SPLIT_NO_EMPTY);
		// print result array
		// print_r($in);
		// build and return the values to replace with
		// state & country variable texts
		switch ($in[0]) {
			// mail configurable flags
			case "COUNTRY":
				return $GLOBALS["country"];
				break;
			case "STATE":
				return $GLOBALS["state"];
				break;
			case "DISTANCE":
				return number_format($GLOBALS["recipient"][14],0);
				break;
			case "TAFURL":
				return $GLOBALS["tafurl"];
				break;
			case "TAFIMG":
				return $GLOBALS["tafimg"];
				break;
			case "LOGO":
				return $GLOBALS["formlogo"];
				break;
			case "FORMNAME":
				return $GLOBALS["formname"];
				break;
			case "CRONLRSUBJECT":
				if ($GLOBALS["limit"]=="UPPER") {
					return "%CONFIGTEXT Cron | Supplier | Notification | Header | Low%";
				} elseif ($GLOBALS["limit"]=="LOWER") {
					return "%CONFIGTEXT Cron | Supplier | Notification | Header | Very Low%";
				}
				break;
			case "CRONSTATESUBJECT":
				if ($GLOBALS["limit"]=="MONTHLY") {
					if ($GLOBALS["recipient"][10] == 0) {
						return "%CONFIGTEXT Cron | Supplier | Statement | Header | Monthly | Premium%";
					} else {
						return "%CONFIGTEXT Cron | Supplier | Statement | Header | Monthly | Freemium%";
					}
				} elseif ($GLOBALS["limit"]=="WEEKLY") {
					if ($GLOBALS["recipient"][10] == 0) {
						return "%CONFIGTEXT Cron | Supplier | Statement | Header | Weekly | Premium%";
					} else {
						return "%CONFIGTEXT Cron | Supplier | Statement | Header | Weekly | Trial%";
					}
				} elseif ($GLOBALS["limit"]=="MANUAL") {
					if ($GLOBALS["recipient"][10] == 0) {
						return "%CONFIGTEXT Cron | Supplier | Statement | Header | Monthly | Premium%";
					} else {
						return "%CONFIGTEXT Cron | Supplier | Statement | Header | Monthly | Freemium%";
					}
				}
				break;
			case "CRONSTATEMESSAGE":
				if ($GLOBALS["limit"]=="MONTHLY") {
					return "%CONFIGTEXT Cron | Supplier | Statement | Message | Monthly | Premium%";
				} elseif ($GLOBALS["limit"]=="WEEKLY") {
					return "%CONFIGTEXT Cron | Supplier | Statement | Message | Weekly | Premium%";
				} elseif ($GLOBALS["limit"]=="MANUAL") {
					if (strlen($_POST["textarea_transactionmessage"]) > 0) {
						return preg_replace(array("/\n\r/","/\n/"),"<br />\n",xmlentities($_POST["textarea_transactionmessage"]));
					} else {
						return "%CONFIGTEXT Cron | Supplier | Statement | Message | Weekly | Premium%";
					}
				}
				break;
			case "CRONSTATEFMESSAGE":
				if ($GLOBALS["limit"]=="MONTHLY") {
					if ($GLOBALS["recipient"][10] == 4) {
						return "%CONFIGTEXT Cron | Supplier | Statement | Message | Monthly | Freemium%";
					} elseif ($GLOBALS["recipient"][10] == 2) {
						return "%CONFIGTEXT Cron | Supplier | Statement | Message | Monthly | Trial%";
					}
				} elseif ($GLOBALS["limit"]=="WEEKLY") {
					if ($GLOBALS["recipient"][10] == 4) {
						return "%CONFIGTEXT Cron | Supplier | Statement | Message | Weekly | Freemium%";
					} elseif ($GLOBALS["recipient"][10] == 2) {
						return "%CONFIGTEXT Cron | Supplier | Statement | Message | Weekly | Trial%";
					}
				} elseif ($GLOBALS["limit"]=="MANUAL") {
					if (strlen($_POST["textarea_transactionmessage"]) > 0) {
						return preg_replace(array("/\n\r/","/\n/"),"<br />\n",xmlentities($_POST["textarea_transactionmessage"]));
					} else {
						if ($GLOBALS["recipient"][10] == 4) {
							return "%CONFIGTEXT Cron | Supplier | Statement | Message | Monthly | Freemium%";
						} elseif ($GLOBALS["recipient"][10] == 2) {
							return "%CONFIGTEXT Cron | Supplier | Statement | Message | Monthly | Trial%";
						}
					}
				}
				break;
			case "LEADSLIST":
				return $GLOBALS["transactions"];
				break;
			case "LIMITMESSAGE":
				if ($GLOBALS["limit"]=="UPPER") {
					return "%CONFIGTEXT Cron | Supplier | Notification | Message | Low%";
				} elseif ($GLOBALS["limit"]=="LOWER") {
					return "%CONFIGTEXT Cron | Supplier | Notification | Message | Very Low%";
				}
				break;
			case "RESPONSESIGNATURE":
				return "%CONFIGTEXT Email | Consumer | Message | Signature%";
				break;
			case "PREMIUMSUBJECT":
				return "%CONFIGTEXT Email | Supplier | Premium | Header%";
				break;
			case "PREMIUMINTRO":
				return "%CONFIGTEXT Email | Supplier | Premium | Message | Intro%";
				break;
			case "PREMIUMSIGNATURE":
				return "%CONFIGTEXT Email | Supplier | Premium | Message | Signature%";
				break;
			case "FREEMIUMSUBJECT":
				return "%CONFIGTEXT Email | Supplier | Freemium | Header%";
				break;
			case "FREEMIUMINTRO":
				return "%CONFIGTEXT Email | Supplier | Freemium | Message | Intro%";
				break;
			case "FREEMIUMSIGNATURE":
				return "%CONFIGTEXT Email | Supplier | Freemium | Message | Signature%";
				break;
			case "RESPONSEMESSAGES":
				$msg = ((join("",$GLOBALS["suppliers"]))?"    %CONFIGTEXT Email | Consumer | Message | Intro Premium%":"").
					((join("",$GLOBALS["supplierp"]))?"    %CONFIGTEXT Email | Consumer | Message | Intro Freemium%":"");
				return $msg;
				break;
			// attribute flags
			case "ATTRIBUTE":
				return $GLOBALS["attribsarr"][$in[1]];
				break;
			// survey flags
			case "SURVEYHEADER":
				return "%CONFIGTEXT Email | Consumer | Survey | Header%";
				break;
			case "SURVEYINTRO":
				return "%CONFIGTEXT Email | Consumer | Survey | Intro%";
				break;
			case "SURVEYSIGNATURE":
				return "%CONFIGTEXT Email | Consumer | Survey | Signature%";
				break;
			case "SURVEYTYPE":
				return xmlentities($GLOBALS["surveytype"]);
				break;
			case "SURVEYURL":
				return $GLOBALS["surveyurl"];
				break;
			case "SURVEYQUESTIONS":
				return $GLOBALS["surveyquestions"];
				break;
			case "SURVEYQCOUNT":
				return $GLOBALS["qcnt"];
				break;
			// consumer flags
			case "CONSUMERNAME":
				return xmlentities($GLOBALS["consumerfullname"]);
				break;
			case "CONSUMEREMAIL":
				return $GLOBALS["consumeremail"];
				break;
			case "CONSUMERCELL":
				return $GLOBALS["consumercell"];
			case "CONSUMERCITY":
				return xmlentities($GLOBALS["city_town"]);
				break;
			case "MESSAGE":
				return xmlentities($GLOBALS["message"]);
				break;
			// expected by date
			case "EXPECTEDBY":
				//echo $GLOBALS["exby"]."\n";
				$expected = explode(" ",date($GLOBALS["dtmstyle"],strtotime($GLOBALS["exby"])));
				//print_r(explode(" ",date($GLOBALS["dtmstyle"],strtotime($GLOBALS["exby"]))));
				return $expected[$GLOBALS["dtlast"]];
				break;
			// supplier flags
			case "SUPPLIERNAME":
				return xmlentities($GLOBALS["recipient"][1]);
				break;
			case "CONTACTFNAME":
				$fname = explode(" ",$GLOBALS["recipient"][0]);
				return xmlentities($fname[0]);
			case "CONTACTNAME":
				return xmlentities($GLOBALS["recipient"][0]);
				break;
			case "ACCMGR":
				return xmlentities($GLOBALS["recipient"][11]);
				break;
			case "ACCMGRMAIL":
				return $GLOBALS["recipient"][12];
				break;
			case "PAYGBALANCE":
				if ($GLOBALS["leadid"]) {
					return $GLOBALS["newbalance"];
				} else {
					return $GLOBALS["balance"];
				}
			case "INVOICEAMOUNT":
				return $GLOBALS["recipient"][13];
				break;
			// lead flags
			case "LEADREFERENCE":
				if ($GLOBALS["prospecting"]) {
					return ($GLOBALS["leadid2"] + 9001100).append0($GLOBALS["ordinal"],2);
				} else {
					return ($GLOBALS["leadid"] + 11001000).$GLOBALS["ordinal"];
				}
				break;
			case "LEADSUCCESS":
				if ($GLOBALS["leadsuccess"]) {
					return "SENT   ";
				} else {
					return "UNSENT";
				}
				break;
			case "LEADCREATED":
				return $GLOBALS["leadcreated"];
				break;
			case "LEADCATEGORY":
				return $GLOBALS["cat"];
				break;
			case "PAYGCOST":
				return $GLOBALS["servicecost"];
				break;
			case "DUPLICATES":
				return $GLOBALS["append"];
				break;
			// other flags
			case "PROSPECTMESSAGE":
				return $GLOBALS["pmsg"];
				break;
			case "SUPPLIERLIST":
				return join("",$GLOBALS["suppliers"]);
				break;
			case "PROSPECTLIST":
				return join("",$GLOBALS["supplierp"]);
				break;
			case "SERVICENAME":
				return xmlentities($GLOBALS["servicename"]);
				break;
			case "REGIONNAME":
				return $GLOBALS["regionname"];
				break;
			// cronjob flags
			case "FROMDATE":
				return date($GLOBALS["dtmstyle"],strtotime($GLOBALS["f"]));
				break;
			case "TODATE":
				return date($GLOBALS["dtmstyle"],strtotime($GLOBALS["t"]));
				break;
			// configurable text flags ADD PARENT REGIONS OR INTO SELECT.
			case "CONFIGTEXT":
				$supplier = getconstraints(($GLOBALS["recipient"][6])?$GLOBALS["recipient"][6]:0,1);
				$regions = getconstraints(($GLOBALS["region"])?$GLOBALS["region"]:0,0);
				$service = getconstraints(($GLOBALS["service"])?$GLOBALS["service"]:0,2);
				$constraints = array();
				if (count($supplier)) array_push($constraints,(count($supplier)>1)?"(".implode(" OR ",$supplier).")":$supplier[0]);
				if (count($regions)) array_push($constraints,(count($regions)>1)?"(".implode(" OR ",$regions).")":$regions[0]);
				if (count($service)) array_push($constraints,(count($service)>1)?"(".implode(" OR ",$service).")":$service[0]);
				$tsql = "SELECT * 
						 FROM 
							18_configurabletexts 
						 WHERE 
						 	18_configurabletexts.text_TextDescription = \"".$in[1]."\" AND 
							(".implode(" AND ",$constraints).");";
				$result = mysql_query_errors($tsql, $conn, __FILE__, __LINE__ , true );
				if ($result) {
					if ($row = mysql_fetch_array($result)) {
						return $row["text_TextFullContent"];
					}
				}
		}
	}
}
// xmlentities function also from global include
function xmlentities($value) {
	// replace characters
	if (is_array($value)) {
		foreach ($value as $key => $val) {
			$value[$key] = xmlentities($val);
		}
	} else {
		//replace $value's invalid xml characters with entities and unicode characters
		// used with Hex NCR's (Hexadecimal Numeric Character Representations).
		$patterns = array(
				'&',			// ampersand symbol
				'<',			// less than symbol
				'>',			// greater than symbol
				'"',			// double quotation mark
				'©',			// (c) copyright symbols
				'ë',			// e with diaresis
				'è',			// e with grave
				'é',			// e with acute
				'"',			// left slanting double quotation mark
				'"'			// right slanting double quotation mark
			);
		$replacements = array(
				'&amp;',
				'&lt;',
				'&gt;',
				'&quot;',
				'&#x00A9;',
				'&#x00EB;',
				'&#x00E8;',
				'&#x00E9;',
				'&#x201C;',
				'&#x201D;'
			);
		$value = utf8_encode($value);	// convert string to unicode to replace characters
		$value = str_replace($patterns, $replacements, $value);
	}
	return $value;
}

Open in new window

Error: undefined entity
Source File: https://www.intellisource.co.za/test_8347379386/scripts/ajax_leads.php?q=0&o=0&m=2011-10-24%2000%3A00%3A00%20to%202011-10-30%2023%3A59%3A59
Line: 10, Column: 22
Source Code:
   <header>#110010451&nbsp;&nbsp;&nbsp;SENT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2011-10-29&nbsp;09:48:53&nbsp;&nbsp;&nbsp;Pierre&nbsp;du&nbsp;Toit&nbsp;(&nbsp;pierre@greywacke.co.za&nbsp;)</header>
0
 
Ray PaseurCommented:
0
 
intellisourceAuthor Commented:
unfortunately i could not use &nbsp; as noted in my previous post - instead i used &#0x00A0;
thanks ray_paseur - this has been the most straightforward way to do it. :)
as can be seen in the screenshot i will post as a reply - it is working now! :D
sincerely, Pierre.
0
 
intellisourceAuthor Commented:
lol i already got the answer on the following page:
http://www.cs.sfu.ca/~ggbaker/reference/characters/#char00A0 ;)
lead-manager-4.jpg
0
 
intellisourceAuthor Commented:
still, thanks a lot for the help every body! :)
0
 
Ray PaseurCommented:
I think your character-set encodings must be wrong, and I am trying to figure out why you need UTF-8.  I extracted the xmlentities function from the code snippet at ID:37057032 and added some data visualization to it.  It does not appear to work correctly in either ISO-8859-1 or UTF-8.  You can install this little test script and run it to see what it is doing to the data.  Try it this way once, then make a change on line 7 to charset=utf-8 and you will see that it has serious issues.

Can you please tell us what characters are causing you to need UTF-8?  Thanks, ~Ray
<?php // RAY_temp_intellisource.php ?>

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>

<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />

<?php
error_reporting(E_ALL);
echo "<pre>";

// EXTRACTED FROM THE POST AT EE
// xmlentities function also from global include
function xmlentities($value)
{
    // replace characters
    if (is_array($value))
    {
        foreach ($value as $key => $val)
        {
            $value[$key] = xmlentities($val);
        }
    }
    else
    {
        //replace $value's invalid xml characters with entities and unicode characters
        // used with Hex NCR's (Hexadecimal Numeric Character Representations).
        $patterns = array(
                '&',            // ampersand symbol
                '<',            // less than symbol
                '>',            // greater than symbol
                '"',            // double quotation mark
                '©',            // (c) copyright symbols
                'ë',            // e with diaresis
                'è',            // e with grave
                'é',            // e with acute
                '"',            // left slanting double quotation mark
                '"'             // right slanting double quotation mark
            );
        $replacements = array(
                '&amp;',
                '&lt;',
                '&gt;',
                '&quot;',
                '&#x00A9;',
                '&#x00EB;',
                '&#x00E8;',
                '&#x00E9;',
                '&#x201C;',
                '&#x201D;'
            );
        // ADD DATA VISUALIZATION
        var_dump($patterns);
        var_dump($replacements);

        $value = utf8_encode($value);    // convert string to unicode to replace characters
        $value = str_replace($patterns, $replacements, $value);
    }
    return $value;
}

// TEST THE FUNCTION
$x = '&<>"©ëèé""';
print_r(xmlEntities($x));

Open in new window

0
 
Ray PaseurCommented:
OK, great!  best of luck with it, ~Ray
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now