Solved

php explode

Posted on 2016-10-11
3
50 Views
Last Modified: 2016-10-12
I have this code snippet:
echo "firm addr2 = " . $_SESSION['bfaddr2'] . "<br>";
$ad2 = explode(" ", $_SESSION['bfaddr2']);
echo "zip = " . $ad2[2] . "<br>";

Open in new window


It produces this:

firm addr2 = Escondido, CA 92029
zip =

How can $ad2[2]  = blank?
0
Comment
Question by:Richard Korts
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41839280
The explode() function is looking for a single " " however there are many characters that can look like a single blank.  Two blanks, for instance.  A tab character, too.  The browser collapses all whitespace to look like a single blank, but the PHP function actually requires a single blank.

Try using var_dump() to print out the data fields that are in play.  Then use your browser's "view source" to see what is really in there.

See example #4 of preg_replace() to learn how to normalize the data before using explode()
1
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41839575
Based on your response
<?php
$x = 'Escondido, CA 92029';
$y = explode(' ', $x);
echo "<pre>" . print_r($y, true) . "</pre>";

Open in new window

Produces
Array
(
    [0] => Escondido,
    [1] => CA
    [2] => 92029
)

Open in new window

So the example posted is not representative of the data used that causes the issue.

Consider this example
<?php
$x = 'Escondido,     CA 92029';
$y = explode(' ', $x);
echo "<pre>" . print_r($y, true) . "</pre>";

Open in new window

Output
Array
(
    [0] => Escondido,
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => CA
    [6] => 92029
)

Open in new window

Now consider this example. Note we do a regular expression replace on any combination of 1 or more space characters (' ', '\t' '\r' '\n') substituting the pipe character (|). It does not have to be the pipe - you can choose any character that will not ever be part of the data.
<?php
$x = preg_replace('/\s+/', '|', 'Escondido,     CA 92029');
$y = explode('|', $x);
echo "<pre>" . print_r($y, true) . "</pre>";

Open in new window

Output
Array
(
    [0] => Escondido,
    [1] => CA
    [2] => 92029
)

Open in new window

0
 

Author Closing Comment

by:Richard Korts
ID: 41841127
preg_replace() perfect!!

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
incrementing radio button names and ids using php 5 37
WordPress syntax 2 25
get domain with php 7 19
Session timeout 5 13
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now