Showing posts with label PHP preg_replace. Show all posts
Showing posts with label PHP preg_replace. Show all posts

Friday, 2 August 2019

How to remove all characters exept numbers from a string using PHP

This is really easy, we will make use of the preg_replace PHP Function which is 100% compatible with PHP 5.3
Example: 
$string = 'This is my phone number: +28 (0)212 1234!!! '
 
$numberString = preg_replace('[\D]', '', $string);
 
echo $numberString; //Will output 2802121234
This is a good way to format phone numbers etc.
The regular expression character \D means to match a character that’s not a digit

How to remove all white spaces within a string using PHP

There are two functions that could be used namely str_replace and  preg_replace.
First, we’ll give an example using str_replace. str_replace is the preferred method since it’s faster and easier to use than preg_replace which relies on regular expressions.
$string =  "This is a test string";
$new_string = str_replace(' ','',$string);
echo $new_string; //This will output Thisisateststring
preg_replace (PHP 4, PHP 5) – http://php.net/manual/en/function.preg-replace.php
$string =  "This is a test string";
$new_string = preg_replace('/( *)/','',$string);
echo $new_string; //This will output Thisisateststring
or
 $string =  "This is a test string";
 $new_string = preg_replace('/\s/','',$string); // \s means strip all white spaces
 echo $new_string; //This will output Thisisateststring

Monday, 24 September 2018

PHP – parse text and convert urls into hyperlinks

The following function will parse a given text and convert all the urls into links. It does this using regular expressions. It converts email addresses to mailto links as well.

Code

1
2
3
4
5
6
7
function parse_links($str)
{
    $str = str_replace('www.', 'http://www.', $str);
    $str = preg_replace('|http://([a-zA-Z0-9-./]+)|', '<a href="http://$1">$1</a>', $str);
    $str = preg_replace('/(([a-z0-9+_-]+)(.[a-z0-9+_-]+)*@([a-z0-9-]+.)+[a-z]{2,6})/', '<a href="mailto:$1">$1</a>', $str);
    return $str;
}

Wednesday, 3 June 2015

PHP: Unzip a Zip File

 This code allows you to Unzip a ZIP file with PHP.
<?php   
function unzip($location,$new_location){
    if(exec("unzip $location",$arr)){
        mkdir($new_location);
        for($i = 1;$i< count($arr);$i++){
            $file = trim(preg_replace("~inflating: ~","",$arr[$i]));
                    copy($location."/".$file,$new_location."/".$file);
                    unlink($location."/".$file);
            }
        return true;
    }
    return false;      
}
 
// usage of this code
if(unzip('ziped_files/test.zip','unziped_files/newfile')){
    echo 'Successfully unzipped!';
}else{
    echo 'Error while processing your file!';
 
}
?>

Monday, 23 February 2015

PHP: Highlight specific words in a phrase

Sometimes, for example, when displaying search results, it is a great idea to highlight specific words. This is exactly what the following function can do:

<?php

function highlight($sString, $aWords) {

 if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
  return false;
 }

 $sWords = implode ('|', $aWords);
  return preg_replace ('@\b('.$sWords.')\b@si', '<strong style="background-color:yellow">$1</strong>', $sString);
}
?>