SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regex to select php from a string

    I'm trying to write a regex to select php from a string that starts with <? and ends with ?>.

    I've got: (<\?[\d\w\s]*\?>)

    This works fine if the php in the string is <? echo 'test'; ?> but if the code is more complicated it breaks.

    Thanks!

  2. #2
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP starts with <?php -- not <?

    Is there not a better way to do whatever you want to do? Sounds quite... clumsy.

    preg_match_all('~<\?php(.*?)\?>~s', $text, $matches);

    Try that and if it doesnt work, what breaks it?
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A script which has a litteral <? or ?> embedded in a string would break the regex.
    You come across this often in scripts which use eval(), or try generate php code and write it to a file for later execution.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <? is shorthand for <?php..

    This seems to be working. Its reading it twice though. The first value in the array has <? ?> then the second value in the array is the same php without the <? and ?>.

    What does the s after ~ do?

    PHP Code:
    $var preg_split('~(<\?(.*?)\?>)~s'$var, -1PREG_SPLIT_DELIM_CAPTURE PREG_SPLIT_NO_EMPTY); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's actually what I'm trying to do crmalibu. I'm trying to split the string up to use eval() on the parts that contain php.

  6. #6
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Brad080283 View Post
    <? is shorthand for <?php..
    No. <? is an incredibly stupid shorthand that shouldn't exist. <?php is the only way to write it.

    This seems to be working. Its reading it twice though. The first value in the array has <? ?> then the second value in the array is the same php without the <? and ?>.
    :rtfm: http://uk2.php.net/preg_match_all

    What does the s after ~ do?
    Makes . match new lines.
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No. <? is an incredibly stupid shorthand that shouldn't exist. <?php is the only way to write it.
    That's your opinion.. You know what they say about opinions..

    I know what it does. If you look at the code I provided I'm not even using preg_match_all. I'm using preg_split and either way it still doesn't explain whats happening to the array.

    And last.. If you're going to help someone try not being a dick next time. I think I can figure it out on my own. Thanks.

  8. #8
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Brad080283 View Post
    That's your opinion.. You know what they say about opinions..
    And some opinions are right. (Mine, not yours, if you're wondering.)

    I know what it does. If you look at the code I provided I'm not even using preg_match_all. I'm using preg_split and either way it still doesn't explain whats happening to the array.

    And last.. If you're going to help someone try not being a dick next time. I think I can figure it out on my own. Thanks.
    If you want help and someone gives you the code, try using it and finding out what it does instead of whining.

    PS: 'Creative' really is a great word to describe that link in your signature. A single image that doesn't do anything is uber creative for a web dev company
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're an asset to the community!

    Have a nice day bud.

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wibble wobble View Post
    And some opinions are right. (Mine, not yours, if you're wondering.)
    Opinions are never right or wrong, that is why they are opinions.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  11. #11
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Opinions are never right or wrong, that is why they are opinions.
    In my opinion 2 is greater than 1. My opinion is right or wrong?
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  12. #12
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wibble wobble View Post
    In my opinion 2 is greater than 1. My opinion is right or wrong?
    2 greater then 1 is a fact not an opinion. Your distaste for the shorthand version of "<? ?>" is an opinion. You should try and get along with those that have a different idea then you instead.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  13. #13
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    2 greater than 1 is a fact AND an opinion. So is <? being a stupid shorthand.
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  14. #14
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you know the mathematical proof to demonstrate that 2 is greater than 1, then it's a fact; otherwise it is only a belief/opinion.

    And the <? shorthand may or may not work depending on php.ini settings, whereas <?php should always work.
    Therefore, it is a fact that <?php is better than <?

    And that's my opinion
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  15. #15
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You seem to be confused greatly, wibble_wobbie. But atlas this is the wrong place for such a discussion, have fun in your own personal universe.

    As for the OP's question,
    PHP Code:
    <?php

    $t 
    = <<<EOGFS
    <?
      // stuff!
    ?>

    <?
      // more stuff!
    ?>
    EOGFS;

    preg_match_all'/<\?(?:php)?\s*(.*?)\s*\?>/s'$t$r );
    var_dump$r[1] );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  16. #16
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help logic. Works great!

  17. #17
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mark Baker View Post
    If you know the mathematical proof to demonstrate that 2 is greater than 1, then it's a fact; otherwise it is only a belief/opinion.

    And the <? shorthand may or may not work depending on php.ini settings, whereas <?php should always work.
    Therefore, it is a fact that <?php is better than <?

    And that's my opinion
    And considering both of them start with the string <? that the OP asked about, it's also irrelevant in this case.
    |
    Add Your Robotics site to my Robot Directory: submit
    |
    Add Your Las Vegas Website to my Las Vegas Directory: submit


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
  •