Solved

php explode

Posted on 2016-10-11
3
38 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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

10 Experts available now in Live!

Get 1:1 Help Now