Solved

php explode

Posted on 2016-10-11
3
79 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 110

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 56

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.

726 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