Answers for "regex php"

PHP
3

regex php

<?php
// First Verif your regex code with https://regex101.com/
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo preg_match($pattern, $str); // Outputs 1

// test email with REGEX
if (!preg_match("/[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+.[a-zA-Z]{2,4}/", $emailAddress)){
    //Email address is invalid.
}

// use filter var to valide Email
if(filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) 
{
     //The email address is valid.
} else{
     //The email address is invalid.
}


?>
Posted by: Guest on November-02-2020
2

preg_match in php

<?php
//Syntex : int preg_match( $pattern, $input, $matches, $flags, $offset)
  
// Declare a variable and initialize it 
$str = "Check For Testing."; 
  
// case-Insensitive search for the word "Check" 
if (preg_match("/\bCheck\b/i", $str, $match))  
    echo "Matched!"; 
else
    echo "not matched";   
  

// Output : Matched
?>
Posted by: Guest on June-19-2020
1

find substring regx php

if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
Posted by: Guest on May-26-2020
0

preg_match

if(!preg_match('/^\[a-zA-Z]+$/',$input)) {
   // String contains not allowed characters ...
}
Posted by: Guest on March-23-2020
1

php preg match space or start of string

/\b(stackoverflow)\b/
Posted by: Guest on September-09-2020
0

regex php

ive never used regex expressions till now and had loads of difficulty trying to convert a [url]link here[/url] into an href for use with posting messages on a forum, heres what i manage to come up with:

$patterns = array(
            "/\[link\](.*?)\[\/link\]/",
            "/\[url\](.*?)\[\/url\]/",
            "/\[img\](.*?)\[\/img\]/",
            "/\[b\](.*?)\[\/b\]/",
            "/\[u\](.*?)\[\/u\]/",
            "/\[i\](.*?)\[\/i\]/"
        );
        $replacements = array(
            "<a href=\"\\1\">\\1</a>",
            "<a href=\"\\1\">\\1</a>",
            "<img src=\"\\1\">",
            "<b>\\1</b>",
            "<u>\\1</u>",
            "<i>\\1</i>"
            
        );
        $newText = preg_replace($patterns,$replacements, $text);

at first it would collect ALL the tags into one link/bold/whatever, until i added the "?" i still dont fully understand it... but it works :)
Posted by: Guest on August-13-2021

Browse Popular Code Answers by Language