dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: PHP Reges not working

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Posts
    97

    PHP Reges not working

    PHP Code:
    <html>
        <head>
        
        </head>
        <body>
    2) Create a string, search for a word in that string. If the word is found display word found. If not found display a message saying word not found.<br />
    <?php
                $subject 
    "I learn web development ";
                
    $pattern '/web/'// I have also tried /^web$/ and /^web/
                
    preg_match($pattern$subject$matches);
                echo 
    $matches[0];
            
    ?></body>
    </html
    ss
    When I use this code, I am getting output as follows
    2) Create a string, search for a word in that string. If the word is found display word found. If not found display a message saying word not found.
    Array

    Please suggest whats wrong and why am I not getting output as "web"

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,303
    Works fine for me. You don't by any chance actually have a 4th parameter in your call to preg_match(), do you, as that would change the structure of the $matches array. You could try something like this to get some more info:
    PHP Code:
    <html>
        <head>

        </head>
        <body>
    2) Create a string, search for a word in that string. If the word is found display word found. If not found display a message saying word not found.<br />
    <?php
                $subject 
    "I learn web development ";
                
    $pattern '/web/'// I have also tried /^web$/ and /^web/
                
    if(preg_match($pattern$subject$matches)) {
                    if( ! 
    is_string($matches[0])) {
                        echo 
    "<pre>DEBUG\n".var_export($matches,true)."</pre>\n";
                    }
                    else {
                        echo 
    $matches[0];
                    }
                }
                else {
                    echo 
    "[not found]";
                }
            
    ?></body>
    </html>
    PS: For something like this where you do not need the power of regular expressions, strpos() or stripos() along with substr() would probably be more efficient.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Jun 2009
    Posts
    97
    <html>
    <head>

    </head>
    <body>
    2) Create a string, search for a word in that string. If the word is found display word found. If not found display a message saying word not found.<br />
    <?php
    $subject = "I learn web development ";
    $pattern = '/^web$/';
    preg_match($pattern, $subject, $matches);
    echo $matches[0];
    ?></body>
    </html >

    It does non work with this regex

    I am getting following output
    Notice: Undefined offset: 0 in C:\wamp\www\PHP_Assignment_3_StringAssignments.php on line 23

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,290
    Your pattern is wrong ! Replace It by '/web/';

    '/^web/' matches web only at the beginning of the string,
    '/web$/' matches web only at the end of the string.
    Then '/^web$/' matches only web in 'web' string !

  5. #5
    Join Date
    Jun 2009
    Posts
    97
    I see.
    I want to match only web word in entire string. If I make pattern as /web/, then it will match web anywhere in the string like webdevelopment, spiderweb etc. But I want only web word, mentioned separately in the entire string to be matched. How to do that ?

    Thanks

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,303
    PHP Code:
    '/\bweb\b/i' 
    "\b" is "word boundary"
    "i" after the closing delimiter is case-insensitive modifier
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles