problem with string

befidled
befidled used Ask the Experts™
on
I'm using the following code where {system_details} is template variable that is actually equal to the commented string below:

$string = "{system_details}";
//$string = "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware";
$array = preg_split("/[\s]*[,][\s]*/", $string);
print_r($array);		

$count = count($array);
		
for ($i = 0; $i < $count; $i++) {
echo "<li>{$array[$i]}</li>";
}

Open in new window


The problem is it's not splitting it at all. so instead of returning:
Array ( [0] => Pistons [1] => Cylinders [2] => Collar Assemblies [3] => Brace Assemblies [4] => Crank Assemblies [5] => Hooks and related hardware ) 

Open in new window


it returns:
Array ( [0] => Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware ) 

Open in new window


What is keeping it from splitting the elements? Is it not seeing it as the correct string type? If I swap out the template variable with the text it works as it is supposed to.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
This works perfectly for me.
Try it with no other code.

Or in your code, try

echo $string;

To see what $string is actually being set to.
<?php
$string = "{system_details}";
$string = "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware";
$array = preg_split("/[\s]*[,][\s]*/", $string);
print_r($array);		

$count = count($array);
		
for ($i = 0; $i < $count; $i++) {
echo "<li>{$array[$i]}</li>";
}
?>

Open in new window

Author

Commented:
That's what's strange. When I echo $string it returns "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware". w/o the quotes.

But when it goes to build the array, it doesn't seem to apply the preg_split so it only creates one Array element.
HainKurtSr. System Analyst

Commented:
what happens if you use

$array = preg_split("/[\s,]+/", $string);
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
it works fine if you uncomment $string. But when it's inserting the template variable it doesn't work. Is it maybe not seeing it as a string?

Author

Commented:
@HainKurt: the problem with that expression is it breaks every word into an element instead of putting each set of words into an element. So for instance "Crank Assemblies" needs to be in on Array element and cant' be split into "Crank" and "Assemblies"

Author

Commented:
@HainKurt: and it doesn't solve the problem. The preg_split seems to work fine, it's that for some reason when I pass the template variable to it, it isn't being seen as a string.
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
Try this

$string = "{system_details}";
//$string = "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware";
$string = preg_replace("/\xa0/"," ",$string);
$array = preg_split("/[\s]*[,][\s]*/", $string);

Author

Commented:
@cyberkiwi: that gives me the same results.

:(
HainKurtSr. System Analyst

Commented:
try this

$string = (string)"{system_details}";

Author

Commented:
@HainKurt: no, still not working.

very strange.
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
Well, it looks like it is not actually spaces in there.
Check all the ascii values.
<?php
$string = "{system_details}";
//$string = "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware";

for ($i = 0; $i < strlen($string); $i++) {
	echo "char $i=" . ord(substr($string, $i)) . "<br/>";
}

$array = preg_split("/[\s]*[,][\s]*/", $string);
print_r($array);		

$count = count($array);
		
for ($i = 0; $i < $count; $i++) {
echo "<li>{$array[$i]}</li>";
}
?>

Open in new window

Author

Commented:
hmmm...

The loop to split it into characters creates this:

Gear

      char 0=123
      char 1=115
      char 2=121
      char 3=115
      char 4=116
      char 5=101
      char 6=109
      char 7=95
      char 8=100
      char 9=101
      char 10=116
      char 11=97
      char 12=105
      char 13=108
      char 14=115
      char 15=125

which translates to: {system_details}

So it's not passing the contents of the variable until it hits the $array = preg_split("/[\s]*[,][\s]*/", $string);

why would that be?

Author

Commented:
I think it's making $string an array.

echo $string[0]; results in {
echo $string[1]; results in s

etc.

Expert of the Quarter 2010
Expert of the Year 2010

Commented:
Do you mind working on this some more?

$string = "{system_details}";
var_dump ($string);

---------
then, separately
---------

$string = "{system_details}";
$string = $string[0];

Author

Commented:
$string = "{system_details}";
var_dump ($string);

var_dupm($string) returns string(16) "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware"
________________________

$string = "{system_details}";
$string = $string[0];
var_dump ($string);

var_dupm($string) returns string(1) "{"
Most Valuable Expert 2011
Top Expert 2016

Commented:
See if this makes sense for your application.  Best, ~Ray
<?php // RAY_temp_split.php
error_reporting(E_ALL);

// TEST DATA FROM EE
$string = "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware";

// MAKE AN ARRAY
$array = explode(',', $string);

// CREATE THE LIST ITEM STATEMENTS
foreach ($array as $item)
{
    echo '<li>' . trim($item) . '</li>' . PHP_EOL;
}

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
And if that does not work for you please post the data that would be in the $string variable and I will try to help you tease it out.

Author

Commented:
@Ray_Paseur: my original code works if I explicitly put the text separated by commas. My problem arises because something is going on with the template variable not being recognized as a string.

So, yes your code works, just as my original code works, if I simply hard code a string of words/phrases separated by commas, however it doesn't work if I use a template variable in that strings place.

does that make sense?
Most Valuable Expert 2011
Top Expert 2016

Commented:
Yes.  Please do this...

echo "<pre>";
var_dump( --> the thing that does not work <--);

and post that output in the code snippet here.  Thanks.  Once we see what the data structure is, we can get past this issue quickly.

Author

Commented:
@Ray_Paseur: So what happens if I hard code my data in my original code.

<ul>      
<li>Pistons</li>
<li>Cylinders</li>
<li>Collar Assemblies</li>
<li>Brace Assemblies</li>
<li>Crank Assemblies</li>
<li>Hooks and related hardware</li>
</ul>

If I use my template variable instead I end up with this:

<ul>      
<li>Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware</li>
</ul>

When I assign my template variable to $string and echo string the result is:
Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware

So from what I can tell, my template variable is not actually getting converted to a string because my preg_split function isn't actually splitting it at all.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Can you please post the template variable, as printed out by var_dump() - there is a reason I want to see the live data as shown by var_dump().

Thanks.
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
Where and what is setting system_details?
I haven't actually seen php decode {} in a string before.  It is passing through some other engine?
Most Valuable Expert 2011
Top Expert 2016

Commented:
PS: Note that there is no preg_split function in my code snippet.  There should not be one in yours, either.  So after you post the var_dump() data, please post the code you are using now so we can catch up on the progress you have made at integrating the suggestions.
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
Ray - see far above.

http://www.experts-exchange.com/help.jsp#hs=30&hi=416

"Read previous posts before commenting"

Author

Commented:
Snippet ID=783544
returns this:

<pre>string(16) "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware"

<li>Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware</li>
<?php
		
//$string = "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware";	

$string = "{system_details}";
echo "<pre>";
var_dump( $string );
		
$array = preg_split("/[\s]*[,][\s]*/", $string);

$count = count($array);
		
for ($i = 0; $i < $count; $i++) {
   echo "<li>{$array[$i]}</li>";
}
		
?>

Open in new window

Expert of the Quarter 2010
Expert of the Year 2010

Commented:
*click*

It's unicode, so your preg_split requires the unicode comma...

Commented:
see code section
the templater is replacing the commas with the ascii for comme '&#44' in the {system_variable} so that when the preg_replace goes through the string, it doesn't come across any commas, but when the php interpreter sends the code back to your browser, the &#44 are all displayed as commas.

so you could either do a $string = html_entity_decode($string) before you send $string to the preg_split, or you could change your regular expression to split on &#44 rather than the comma.

Open in new window

Commented:
the templater is replacing the commas with the ascii for comme '&#44' in the {system_variable} so that when the preg_replace goes through the string, it doesn't come across any commas, but when the php interpreter sends the code back to your browser, the &#44 are all displayed as commas.

so you could either do a $string = html_entity_decode($string) before you send $string to the preg_split, or you could change your regular expression to split on &#44 rather than the comma.

Author

Commented:
@cyberkiwi: it's running through ExpressionEngine.

@maduic: I've tried $string = html_entity_decode($string) but that didn't do anything.

I've also tried some variations (see below, maybe I'm doing this wrong, we're using UTF-8) on the preg_split but haven't had any success:

$array = preg_split("/[\s]*[&#44; ][\s]*/", $string);
$array = preg_split("/[\s]*[&#44][\s]*/", $string);
$array = preg_split("/[\s]*[00101100][\s]*/", $string);
$array = preg_split("/[\s]*[0x2C][\s]*/", $string);

Most Valuable Expert 2011
Top Expert 2016

Commented:
This seems a little counterintuitive to me.

<pre>string(16) "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware"

What that seems to suggest is that the string length is 16 bytes.  Is that what var_dump() printed out?  Seems odd.

I copied the commas in that statement into an editor and converted them to hex.  I got 2C, and IIRC that is the comma.

Commented:
try the following
$string = "{system_details}";
$string = html_entity_decode($string);
$array = preg_split("/[\s]*[,][\s]*/", $string);
print_r($array);		

$count = count($array);
		
for ($i = 0; $i < $count; $i++) {
echo "<li>{$array[$i]}</li>";
}

Open in new window

Author

Commented:
@Ray_Paseur: I copied it directly from the results...

Most Valuable Expert 2011
Top Expert 2016

Commented:
Hmm... This string is 16 characters long:

Is there some other part to this code that you have not shown us yet?
1...5...10...15...
{system_details}

Open in new window

Expert of the Quarter 2010
Expert of the Year 2010

Commented:
In ExpressionEngine, change the ParsingStage to "Input" for {system_details}
(http://expressionengine.com/docs/cp/templates/template_preferences.html)

The fact that var_dump can return

string(16) "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware"

Means that var_dump was trying to output

string(16) {system_details}

But at the output stage, it was captured by ExpressionEngine and turned into

string(16) "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware"

Author

Commented:
@maduic:

Results:

<ul>      
Array
( [0] => Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware
)

<li>Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware</li></ul>

Most Valuable Expert 2011
Top Expert 2016

Commented:
Are you using error_reporting(E_ALL) in this script?  If not, please turn that on.

I am wondering if the term "system_details" has somehow become a defined constant?  You might want to print those out and see what you get.

Author

Commented:
@Ray_Paseur: Full code is below:

it's just my code wrapped in ExpressionEngine's template tags.

@cyberkiwi: I've already got my PHP parsing set to Input.


{exp:weblog:entries weblog="aircraft" url_title="{segment_3}" }
{systems search:system_name="{segment_2}"}
<ul>	

	<?php
		
		$string = "{system_details}";
		$string = html_entity_decode($string);
		$array = preg_split("/[\s]*[,][\s]*/", $string);
		print_r($array);

		$count = count($array);
		
		for ($i = 0; $i < $count; $i++) {
			echo "<li>{$array[$i]}</li>";
		}

		
	?>
</ul>
{/systems}
{/exp:weblog:entries}

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
If you want to run this, I will post the email message back here and we can take a look at it.  I have no way of knowing what the Expression Engine is doing to your data, and I am not sure this will work, but it seems simple enough to try.
{exp:weblog:entries weblog="aircraft" url_title="{segment_3}" }
{systems search:system_name="{segment_2}"}
<ul>	

	<?php
		
		$string = "{system_details}";

// NEW CODE
ob_start();
var_dump($string);
$s = ob_get_clean();
mail('Ray.Paseur@Gmail.com', 'EE', $s);

		$string = html_entity_decode($string);
		$array = preg_split("/[\s]*[,][\s]*/", $string);
		print_r($array);

		$count = count($array);
		
		for ($i = 0; $i < $count; $i++) {
			echo "<li>{$array[$i]}</li>";
		}

		
	?>
</ul>
{/systems}
{/exp:weblog:entries}

Open in new window

Expert of the Quarter 2010
Expert of the Year 2010
Commented:
Okay, different tack.
ExpressionEngine "input" stage refers only to PHP parsing.
It still only parses the expressions at output stage, so we'll make PHP output it and capture it back.

$string = "{system_details}";
ob_start();
echo $string;
$string = ob_get_contents();
ob_end_clean();
//$string = "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware";
$array = preg_split("/[\s]*[,][\s]*/", $string);
print_r($array);
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
Looks like Ray got into the same frame of mind on output parsing, and just seconds before I posted mine.

Commented:
if this is what it takes to perform simple tasks in ExpressionEngine.... then count me OUT on that software

Author

Commented:
@cyberkiwi: That worked!

@maduic: Actually, 99% of the time it does an amazing job. Compared to something like WordPress it is very simple to use, this was just a special case.

Thanks everyone for working so hard to help me!

Final code that works is attached:
$string = "{system_details}";
									ob_start();
									echo $string;
									$string = ob_get_contents();
									ob_end_clean();
									//$string = "Pistons, Cylinders, Collar Assemblies, Brace Assemblies, Crank Assemblies, Hooks and related hardware";
									$array = preg_split("/[\s]*[,][\s]*/", $string);
									
									$count = count($array);
									
									for ($i = 0; $i < $count; $i++) {
										print("<li>{$array[$i]}</li>");
									}

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Agreed - Expression Engine may not be your friend.  @cyberwiki: great minds thinking alike, I believe!

I'll look for that email.

Author

Commented:
@maduic: part of the challenge was that I'm using an ExpressionEngine plugin so I have to deal with both EE and the plugin's way of handling data to get the results I want.

Author

Commented:
Can I split points in half?

Author

Commented:
@Ray_Paseur: No need to email, that solution from both you and cyberkiwi: worked great. I'll have to read up on ob_start(); function.

Thanks!

Author

Commented:
for the record, if I could I'd give you all 1000 points! :)
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
>> @maduic: part of the challenge was that I'm using an ExpressionEngine plugin so I have to deal with both EE and the plugin's way of handling data to get the results I want.

I had to double-take to re-read the sentence for the context of "EE" (this forum).

Happy for the split :)


Regards and have a good day.

Author

Commented:
Thanks again, that was a tough one but I really appreciate you guys digging in to it like you did!

Author

Commented:
@cyberkiwi: that's funny, the ExpressionEngine community also uses EE as an abbreviation...I didn't even think about where I was! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial