?
Solved

php explode

Posted on 2016-10-11
3
Medium Priority
?
99 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 58

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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