Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php explode

Posted on 2016-10-11
3
Medium Priority
?
107 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 59

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

604 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