Questions tagged [preg-replace]

preg_replace() is a PHP function that performs string replacement using regular expressions.

preg_replace() is a function member of Perl Compatible Regular Expressions implementation of .

Check out PCRE in the official PHP documentation

5858 questions
252
votes
15 answers

Remove multiple whitespaces

I'm getting $row['message'] from a MySQL database and I need to remove all whitespace like \n \t and so on. $row['message'] = "This is a Text \n and so on \t Text text."; should be formatted to: $row['message'] = 'This is a Text and so on…
creativz
  • 10,369
  • 13
  • 38
  • 35
151
votes
4 answers

How can I convert ereg expressions to preg in PHP?

Since POSIX regular expressions (ereg) are deprecated since PHP 5.3.0, I'd like to know an easy way to convert the old expressions to PCRE (Perl Compatible Regular Expressions) (preg). Per example, I have this regular expression: eregi('^hello…
netcoder
  • 66,435
  • 19
  • 125
  • 142
128
votes
20 answers

Replacing accented characters php

I am trying to replace accented characters with the normal replacements. Below is what I am currently doing. $string = "Éric Cantona"; $strict = strtolower($string); echo "After Lower: ".$strict; $patterns[0] = '/[á|â|à|å|ä]/'; …
Lizard
  • 43,732
  • 39
  • 106
  • 167
89
votes
8 answers

Remove a part of a string, but only when it is at the end of the string

I need to remove a substring of a string, but only when it is at the END of the string. for example, removing 'string' at the end of the following strings : "this is a test string" -> "this is a test " "this string is a test string" - > "this…
Dylan
  • 9,129
  • 20
  • 96
  • 153
87
votes
3 answers

Replace preg_replace() e modifier with preg_replace_callback

I'm terrible with regular expressions. I'm trying to replace this: public static function camelize($word) { return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word); } with preg_replace_callback with an anonymous function. I don't…
Casey
  • 1,941
  • 3
  • 17
  • 30
71
votes
3 answers

Warning: preg_replace(): Unknown modifier

I have the following error: Warning: preg_replace(): Unknown modifier ']' in xxx.php on line 38 This is the code on line 38:
", "", preg_replace("]*>]*>", "", wp_nav_menu(array('theme_location' =>…
user3122995
  • 739
  • 1
  • 5
  • 4
71
votes
6 answers

Remove all non-alphanumeric characters using preg_replace

How can I remove all non alphanumeric characters from a string in PHP? This is the code, that I'm currently using: $url = preg_replace('/\s+/', '', $string); It only replaces blank spaces.
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
67
votes
4 answers

PHP preg replace only allow numbers

How can I modify this existing preg_replace to only allow numbers? function __cleanData($c) { return preg_replace("/[^A-Za-z0-9]/", "",$c); }
Ryan
  • 14,392
  • 8
  • 62
  • 102
67
votes
3 answers

Replace only first match using preg_replace

I have a string with structure similar to: 'aba aaa cba sbd dga gad aaa cbz'. The string can be a bit different each time as it's from an external source. I would like to replace only first occurrence of 'aaa' but not the others. Is it possible?
deadbeef
  • 673
  • 1
  • 5
  • 5
65
votes
15 answers

Convert plain text URLs into HTML hyperlinks in PHP

I have a simple commenting system where people can submit hyperlinks inside the plain text field. When I display these records back from the database and into the web page, what RegExp in PHP can I use to convert these links into HTML-type anchor…
Volomike
  • 23,743
  • 21
  • 113
  • 209
58
votes
4 answers

How can I get at the matches when using preg_replace in PHP?

I am trying to grab the capital letters of a couple of words and wrap them in span tags. I am using preg_replace for extract and wrapping purposes, but it's not outputting anything. preg_replace("/[A-Z]/", "$1", $str)
Polsonby
  • 22,825
  • 19
  • 59
  • 74
58
votes
17 answers

Replace URLs in text with HTML links

Here is a design though: For example is I put a link such as http://example.com in textarea. How do I get PHP to detect it’s a http:// link and then print it as print "http://www.example.com"; I remember doing…
Angel.King.47
  • 7,922
  • 14
  • 60
  • 85
58
votes
8 answers

PHP replacing special characters like à->a, è->e

I have php document signup.php which save the content from form (in form.php document) to MySQL base. The problem arises when I want to reformat the input content. I want do decode UTF-8 charachters like à->a. $first_name=$_POST['first_name']; …
Zoran Đukić
  • 767
  • 1
  • 6
  • 12
56
votes
9 answers

How do I remove the URL protocol and slash from user input in PHP

Example user input http://example.com/ http://example.com/topic/ http://example.com/topic/cars/ http://www.example.com/topic/questions/ I want a PHP function to make the output…
Blur
  • 1,216
  • 3
  • 12
  • 17
48
votes
1 answer

Replace deprecated preg_replace /e with preg_replace_callback

$result = preg_replace( "/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/iseU", "CallFunction('\\1','\\2','\\3','\\4','\\5')", $result ); The above code gives a deprecation warning after upgrading to PHP…
dima.h
  • 860
  • 2
  • 10
  • 14
1
2 3
99 100