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
26
votes
9 answers

Regex to strip comments and multi-line comments and empty lines

I want to parse a file and I want to use php and regex to strip: blank or empty lines single line comments multi line comments basically I want to remove any line containing /* text */ or multi line comments /*** some text *****/ If possible,…
Ahmad Fouad
  • 3,957
  • 14
  • 45
  • 62
26
votes
2 answers

Remove trailing newline

I've a MySQL database from which I extract a string which is a list of words separated by newline. Now I want to remove only the trailing newline. I tried using preg_replace as $string = preg_replace('/\n/','',$string); It works, but all the…
mathewraj7786
  • 263
  • 1
  • 3
  • 5
25
votes
4 answers

Strip all non-alphanumeric, spaces and punctuation symbols from a string

How can I use PHP to strip out all characters that are NOT letters, numbers, spaces, or punctuation marks? I've tried the following, but it strips punctuation. preg_replace("/[^a-zA-Z0-9\s]/", "", $str);
Tedd
  • 253
  • 1
  • 3
  • 5
24
votes
3 answers

Replacing file content in PHP

I need a function just like preg_replace but instead of strings I need it to work with files / file content.
Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
24
votes
10 answers

Convert string into slug with single-hyphen delimiters only

I would like to sanitize a string in to a URL so this is what I basically need: Everything must be removed except alphanumeric characters and spaces and dashed. Spaces should be converter into dashes. Eg. This, is the URL! must…
Atif
  • 10,623
  • 20
  • 63
  • 96
23
votes
5 answers

Best way to make links clickable in block of text

I want: Here is link: http://google.com And http://example.com inside. And another one at the very end: http://test.net to become: Here is link: http://google.com And
Silver Light
  • 44,202
  • 36
  • 123
  • 164
21
votes
4 answers

regular expression and forward slash

i'm searching for keywords in a string via a regular expression. It works fine for all keywords, exept one which contains a forward slash in it: "time/emit" . Even using preg_quote($find,'/'), which escapes it, i still get the message: Unknown…
pixeline
  • 17,669
  • 12
  • 84
  • 109
21
votes
3 answers

Regular expressions for a range of unicode points PHP

I'm trying to strip all characters from a string except: Alphanumeric characters Dollar sign ($) Underscore (_) Unicode characters between code points U+0080 and U+FFFF I've got the first three conditions by doing…
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
20
votes
8 answers

How do I remove everything after a space in PHP?

I have a database that has names and I want to use PHP replace after the space on names, data example: $x="Laura Smith"; $y="John. Smith" $z="John Doe"; I want it to return Laura John. John
AlphaApp
  • 615
  • 1
  • 7
  • 15
19
votes
3 answers

Regex to only allow alphanumeric, comma, hyphen, underscore and semicolon

I've already got a bit of working code but I need someone to help explain why it works if they can! I am using PHP to replace anything in a string if it is not either a-z, A-Z, 0-9, a comma, a semicolon, an underscore or a hyphen (which ultimately…
Robin
  • 1,602
  • 3
  • 16
  • 24
19
votes
1 answer

Replace an upper-case string with lower-case using preg_replace() and regex

Is it possible to replace an upper-case with lower-case using preg_replace and regex? For example: The following string: $x="HELLO LADIES!"; I want to convert it to: hello ladies! using preg_replace(): echo preg_replace("/([A-Z]+)/","$1",$x);
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
19
votes
7 answers

PHP preg_replace the backslash \

Really simple question: how can I preg_replace the backslash character?
Oliver Bayes-Shelton
  • 6,135
  • 11
  • 52
  • 88
19
votes
3 answers

Regex Optional Groups?

I seem to have confused myself with a preg_match regex I'm doing, so fresh eyes and help would be appreciated. My current regex is as follows: /(.*?)/se I am looking to make the number input and…
Joel
  • 2,185
  • 4
  • 29
  • 56
18
votes
6 answers

Remove
's from the end of a string

As the title says I have a string like this: $string = "Hello World
hello world

"; I would like to get rid of the
s at the end of the string so it looks as follows: $string = "Hello World
hello world"; I tried…
iam_peter
  • 3,205
  • 1
  • 19
  • 31
18
votes
7 answers

Symfony 1.4 using deprecated functions in php 5.5

I recently upgraded PHP from version 5.3.27 to 5.5.0. Everything is working fine in my Symfony 2.3.2 project, and I can enjoy the latest PHP functionalities. Now when I am going back to my other Symfony 1.4.16 project, I get a PHP error about…
mika
  • 1,971
  • 3
  • 18
  • 32