SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Removing Starting and Ending Single Quotes from Variable

    Hello.

    I'm attempting to strip out the starting and ending quotation marks (if any) from a variable using REGEX.

    The following code successfully removes double-quotes;


    Code PHP:
    // Parse Data
    	// $ex_displayname
    			$ex_displayname_PAR = $ex_displayname;
    			$ex_displayname_PAR = preg_replace('/^"/','',$ex_displayname_PAR);
    			$ex_displayname_PAR = preg_replace('/"$/','',$ex_displayname_PAR);

    However the version I attempted for single quotes is doing nothing;

    Code PHP:
    // Parse Data
        // $ex_displayname
                $ex_displayname_PAR = $ex_displayname;
    			$ex_displayname_PAR = preg_replace("/^'/","",$ex_displayname_PAR);
    			$ex_displayname_PAR = preg_replace("/'$/","",$ex_displayname_PAR);

    Can anyone suggest what might be wrong?

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is working fine on my local xampp server and successfully removes the single quotes
    Code:
     
     
                $ex_displayname_PAR = "'qwerty'";
     
                echo '<br />before: '.$ex_displayname_PAR;
     
                $ex_displayname_PAR = preg_replace("/^'/","",$ex_displayname_PAR);
                $ex_displayname_PAR = preg_replace("/'$/","",$ex_displayname_PAR);
     
                echo '<br />after: '.$ex_displayname_PAR;

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    I don't see the need to use regex when you have trim

    PHP Code:
    $ex_displayname_PAR trim($ex_displayname_PAR'"\''); 
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX
    I don't see the need to use regex when you have trim

    HAHAHA!... "Have you tried trim(); ?"


    Thanks for the heads up, I'll give it a go.

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It does work. I was going to make a comment, then thought "Hmm - I better test this before I comment and look like an @rse".
    Ian Anderson
    www.siteguru.co.uk

  6. #6
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon
    this is working fine on my local xampp server and successfully removes the single quotes
    Quote Originally Posted by siteguru
    It does work.
    Really?

    Weird, it's not making any changes at all when I test. Unless there's something else going wrong along the way.

    Anyways, if trim() can do it, that'd be easier; One line of code vs. four.

    Going to try it out in a few minutes.

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    I'm going out on a limb here... but, have you tried trim() ?
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Maybe there are spaces next to the single quotes?
    In that case, you should trim() first to remove the spaces.

  9. #9
    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)
    It shouldn't matter with a suitable character mask.
    PHP Code:
    <?php
    var_dump
    (
      
    trim(' "foo" ''"\' ')
    );

    #string(3) "foo"
    @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.

  10. #10
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    My test was very simple ...
    PHP Code:
    <?php
    echo "<pre>";
    $var " 'My name is John O'Shea, isn't it?' ";
    echo 
    $var "\n";
    $var trim ($var' \'');
    echo 
    $var "</pre>";
    ?>
    Ian Anderson
    www.siteguru.co.uk

  11. #11
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Thanks, everyone. Yes, trim() worked.

    Quote Originally Posted by Salathe
    have you tried trim() ?
    I think that needs to be on a Sitepoint T-shirt.

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse


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
  •