Results 1 to 5 of 5

Thread: Start With and End With

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Start With and End With

    How can I test if string start with "<" and end with ">" and print true if that true.


    $Text="<hi how are you>"; // true

    $Text=" any <hi how are you>"; // false

    $Text="<hi how are you> any"; // false

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Like this:

    Code:
    $s[] = "<boooam>asdf";
    $s[] = "abbb<aoom>";
    $s[] = "<funk>";
    
    for ($i = 0; $i < count($s); $i++) {
        if (preg_match("/^<(.*)>$/i", $s[$i])) {
            echo htmlspecialchars($s[$i]) . " : true<br>\n";
        } else {
            echo htmlspecialchars($s[$i]) . ": false<br>\n";
        }
    }
    Edit: Changed code.
    Last edited by The Hobo; Feb 13th, 2003 at 03:28 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    here we go again

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    here we go again
    lol, that was my first reaction, too.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    Another method:

    PHP Code:
    $text="<hi how are you>";
    if(
    substr($text,0,1) == "<" && substr($text,-1) == ">"
        echo 
    "True<br>";
    else 
        echo 
    "False<br>";

    $text=" any <hi how are you>"// false 
    if(substr($text,0,1)=="<" && substr($text,-1)==">"
        echo 
    "True<br>";
    else 
        echo 
    "False<br>";

    $text="<hi how are you> any"// false
    if(substr($text,0,1)=="<" && substr($text,-1)==">"
        echo 
    "True<br>";
    else 
        echo 
    "False<br>"
    Of course you could also loop through an array, with this function, similar to how Hobo did it above.

    s.

Posting Permissions

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



Click Here to Expand Forum to Full Width