Search
 
SCRIPT & CODE EXAMPLE
 

PHP

preg_replace


<?php
$string = 'April 15, 2003';
$pattern = '/(w+) (d+), (d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
Comment

preg_replace examples

$result = preg_replace('/abc/', 'def', $string);   # Replace all 'abc' with 'def'
$result = preg_replace('/abc/i', 'def', $string);  # Replace with case insensitive matching
$result = preg_replace('/s+/', '', $string);      # Strip all whitespaces
Comment

php preg replace

preg_replace($pattern, $replacement, $subject [, $limit [, &$count]]);
// Returns an array if the subject parameter is an array,
// or a string otherwise.
// If matches are found, the new subject will be returned, otherwise
// subject will be returned unchanged or NULL if an error occurred.
Comment

preg_replace

<?php
$string = 'April 15, 2003';
$pattern = '/(w+) (d+), (d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
?>
Comment

preg_replace examples

$result = preg_replace(
    array('/pattern1/', '/pattern2/'),
    array('replace1', 'replace2'),
    $string
);
Comment

php preg_replace function

preg_replace($pattern, $replacement, $string);
Comment

preg_replace examples

$result = preg_replace('/abc(def)hij/', '/1/', $string);
$result = preg_replace('/abc(def)hij/', '/$1/', $string);
$result = preg_replace('/abc(def)hij/', '/${1}/', $string);
Comment

preg_replace examples

# Strip HTML tag
$result = preg_replace('#<span id="15">.*</span>#m', '', $string);
Comment

preg_replace

i searched for a while for a script, that could see the difference between an html tag and just < and > placed in the text,
the reason is that i recieve text from a database,
wich is inserted by an html form, and contains text and html tags,
the text can contain < and >, so does the tags,
with htmlspecialchars you can validate your text to XHTML,
but you'll also change the tags, like <b> to <b>,
so i needed a script that could see the difference between those two...
but i couldn't find one so i made my own one,
i havent fully tested it, but the parts i tested worked perfect!
just for people that were searching for something like this,
it may looks big, could be done easier, but it works for me, so im happy.

<?php
function fixtags($text){
$text = htmlspecialchars($text);
$text = preg_replace("/=/", "=""", $text);
$text = preg_replace("/"/", """", $text);
$tags = "/<(/|)(w*)( |)(w*)([=]*)(?|(")"""|)(?|(.*)?"(")|)([ ]?)(/|)>/i";
$replacement = "<$1$2$3$4$5$6$7$8$9$10>";
$text = preg_replace($tags, $replacement, $text);
$text = preg_replace("/=""/", "=", $text);
return $text;
}
?>

an example:

<?php
$string = "
this is smaller < than this<br />
this is greater > than this<br />
this is the same = as this<br />
<a href="http://www.example.com/example.php?test=test">This is a link</a><br />
<b>Bold</b> <i>italic</i> etc...";
echo fixtags($string);
?>

will echo:
this is smaller < than this<br />
this is greater > than this<br />
this is the same = as this<br />
<a href="http://www.example.com/example.php?test=test">This is a link</a><br />
<b>Bold</b> <i>italic</i> etc...

I hope its helpfull!!
Comment

How to use preg_replace() function in php

<!DOCTYPE html>
<html>
<body>
  <?php  
  // Display result after replace and count 
  echo preg_replace(array('/d/', '/s/'),
          '*', '1234567890', 8);
  ?>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Php :: laravel foreach localstorage 
Php :: add column in exesting table 
Php :: Generating Random String In PHP Using Brute Force 
Php :: Writing a New Block for Cryptocurrency Blockchain 
Php :: laravel routes options 
Php :: php echo to stderr 
Php :: Call to a member function move() on null 
Php :: oneliner if php 
Php :: how to reduce time taken by php script on server 
Php :: Unable to create PsySH runtime directory 
Php :: $_FILES image dimensions 
Php :: show all errors in php 
Php :: laravel csv import to database 
Php :: refresh_ttl 
Php :: add image thumb on checkout woo 
Php :: data showing in single option instead of multiple option from json array 
Php :: htmlentities (PHP 4, PHP 5, PHP 7, PHP 8) htmlentities — Convert all applicable characters to HTML entities 
Php :: laravel add params form submission 
Php :: select all matched text phpstrom 
Php :: answer to guzzle/psr7 undefine 
Php :: laravel onclick all notification reads 
Php :: add reviews from code site reviews wp 
Php :: trim string in php codeigniter 
Php :: Laravel A row must be an array or a TableSeparator instance. 
Php :: php: Güvenlik Fonksiyonu 
Php :: Submit and draft button use in laravel 
Php :: Terminfo file does not exist. tinker larvel 
Php :: Breaking of code snippets in CKEditor as result code blocks are empty. 
Php :: eloquentdatatable add column 
Php :: how to convert php code to a string 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =