SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Explode unless is a $variable

    Hi, i have this problem:

    PHP Code:
    $variable "Hi i'm a variable";
    $items "String String $variable String";

    explode(" "$items); // I want to explode spaces but not the spaces in $variable 
    Like i said in the comment, i want to protect the $variable from exploding his spaces

    Is that possible ? thanks!

  2. #2
    SitePoint Addict joaquin_win's Avatar
    Join Date
    Jul 2005
    Location
    Venezuela
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use placeholders like in prinf (%s) and then iterate over the array from the explode and replace.

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Joaquin, thanks for replying!

    I don't understand exactly, can you give me a small example ?

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why not use single quotes rather than double so the variable doesn't get evaluated?

    PHP Code:
    $variable "Hi i'm a variable";

    $items 'String String $variable String';



    explode(" "$items); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi SilverBullet thanks for replying!

    Because the $items variable will be a class call parameter and the explode will be inside the class, so i need the $variable's content.

    For example :

    PHP Code:
    $variable "Hi i'm a variable";
    $items 'String String $variable String';

    $add = new Receptor($items);

    class 
    Receptor {

        function 
    __construct($items) {
            
    explode(" "$items);
        }



  6. #6
    SitePoint Addict joaquin_win's Avatar
    Join Date
    Jul 2005
    Location
    Venezuela
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this:

    PHP Code:
    $variable "Hi i'm a variable";
    $items "String String %s String";

    $exploded explode(' '$items);

    foreach(
    $exploded as $i => $value) {
        if (
    $value=='%s'$exploded[$i] = $variable;



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •