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
47
votes
3 answers

Replace all characters except letters, numbers, spaces and underscores

I am looking to replace all characters in a string except letters, numbers, spaces and underscores. Could someone please provide a example?
Daniel Blackmore
  • 481
  • 1
  • 5
  • 6
42
votes
11 answers

Replace tabs and spaces with a single space as well as carriage returns and newlines with a single newline

$string = "My text has so much whitespace Plenty of spaces and tabs"; echo preg_replace("/\s\s+/", " ", $string); I read the PHP's documentation and followed the preg_replace() tutorial, however this code…
Teiv
  • 2,605
  • 10
  • 39
  • 48
42
votes
10 answers

Replace multiple newlines, tabs, and spaces

I want to replace multiple newline characters with one newline character, and multiple spaces with a single space. I tried preg_replace("/\n\n+/", "\n", $text); and failed! I also do this job on the $text for formatting. $text = wordwrap($text, 120,…
Sourav
  • 17,065
  • 35
  • 101
  • 159
42
votes
2 answers

How to use preg_replace_callback?

I have the following HTML statement [otsection]Wallpapers[/otsection] WALLPAPERS GO HERE [otsection]Videos[/otsection] VIDEOS GO HERE What I am trying to do is replace the [otsection] tags with an html div. The catch is I want to increment the id…
Mark
  • 3,653
  • 10
  • 30
  • 62
41
votes
4 answers

PHP preg_replace/preg_match vs PHP str_replace

Can anyone give me a quick summary of the differences please? To my mind, are they both doing the same thing?
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
41
votes
5 answers

PHP using preg_replace : "Delimiter must not be alphanumeric or backslash" error

I am trying to take a string of text like so: $string = "This (1) is (2) my (3) example (4) text"; In every instance where there is a positive integer inside of parentheses, I'd like to replace that with simply the integer itself. The code I'm…
Christopher
  • 411
  • 1
  • 4
  • 3
38
votes
3 answers

Remove first forward slash in a link?

I need to remove the first forward slash inside link formatted like this: /directory/link.php I need to have: directory/link.php I'm not literate in regular expressions (preg_replace?) and those slashes are killing me.. I need your help…
0plus1
  • 4,475
  • 12
  • 47
  • 89
38
votes
8 answers

Java equivalent to PHP's preg_replace_callback

I'm in the process of moving an application from PHP to Java and there is heavy use of regular expressions in the code. I've run across something in PHP that doesn't seem to have a java equivalent: preg_replace_callback() For every match in the…
Mike
  • 8,853
  • 3
  • 35
  • 44
34
votes
1 answer

How can I strip commas and periods from a string?

I have string containing something like this: "Favourite bands: coldplay, guns & roses, etc.," How can I remove commas and periods using preg_replace?
Josh R
  • 1,309
  • 6
  • 19
  • 31
33
votes
4 answers

JavaScript equivalent for PHP preg_replace

I've been looking for a js-equivalent for the PHP preg_replace function and what I found so far is simply string.replace. However I'm not sure how to convert my regular expression to JavaScript. This is my PHP code: preg_replace("/( )*/", $str,…
Skyfe
  • 345
  • 1
  • 3
  • 5
31
votes
8 answers

PHP remove special character from string

I have problems with removing special characters. I want to remove all special characters except "( ) / . % - &", because I'm setting that string as a title. I edited code from the original (look below): preg_replace('/[^a-zA-Z0-9_ -%][().][\/]/s',…
user453089
  • 719
  • 2
  • 13
  • 23
31
votes
5 answers

Are the PHP preg_functions multibyte safe?

There are no multibyte 'preg' functions available in PHP, so does that mean the default preg_functions are all mb safe? Couldn't find any mention in the php documentation.
Spoonface
  • 1,513
  • 1
  • 20
  • 29
30
votes
4 answers

PHP preg_replace: Case insensitive match with case sensitive replacement

I'm using preg_replace in PHP to find and replace specific words in a string, like this: $subject = "Apple apple"; print preg_replace('/\bapple\b/i', 'pear', $subject); Which gives the result 'pear pear'. What I'd like to be able to do is to match…
colin
  • 393
  • 1
  • 3
  • 8
28
votes
2 answers

check if the string contains alphabets in php

How to check if the string has alphabets in PHP The ctype_alpha and ctype_digit doesn't help in it. is their any method?
user1765876
26
votes
2 answers

Remove spaces from the beginning and end of a string

I am pretty new to regular expressions. I need to clean up a search string from spaces at the beginning and the end. Example: " search string " Result: "search string" I have a pattern that works as a javascript solution but I cant get it to work on…
Alex
  • 1,630
  • 1
  • 20
  • 31