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
1
vote
2 answers

How to get a clean string with extension with preg_replace in php?

I'd like to convert this string $text = "TEST#%_'JanE,.-_doc30)/||\.pdf$@!3)"; into a clean string like this TEST_JanE_doc30.pdf So, basically the preg_replace should be: change all non letter and number before the extension into a single…
Shota
  • 515
  • 3
  • 18
1
vote
1 answer

Replace all lines except first

I trying do something like that for example: And I want replace all lines contains IMG with "---" except first. I don't want delete it, just…
Max
  • 33
  • 1
  • 3
1
vote
2 answers

How to remove special characters from file name after upload

I am uploading a zip folder using Laravel after uploading I am extracting this zip folder using the zip archive everything works perfectly but I have a problem with file names in this folder, The extracted folder may contain many files these file…
Salman Iqbal
  • 442
  • 5
  • 23
1
vote
2 answers

escape a + sign in preg_replace

How can I escape this? /{\([^.{}]*)-}/ And this? /{\([^.{}]*)+}/
Vish
  • 4,508
  • 10
  • 42
  • 74
1
vote
1 answer

php: dynamic preg_replace

The code below is not a functioning method it's just written to help you understand what I'm trying to do. // $i = occurrence to replace // $r = content to replace private function inject($i, $r) { // regex matches anything in the format…
kr1zmo
  • 837
  • 3
  • 13
  • 30
1
vote
1 answer

How do I allow _ in this regex for this function

I have a function that works great at converting links, @tags and #hashtags into links, however it does not convert @tags_underscore or #hashtags_underscore, or links with underscore in them into links fully as it leave out the _underscore part of…
SlickRemix
  • 456
  • 6
  • 17
1
vote
2 answers

recreate js regex match function in php

I have a problem with recreation function written in Javascript, to make it work in php. I think that I am really close, but don't understand php syntax that well. So I have something like that in JS: function convertStr(str, id) { const…
piotrruss
  • 417
  • 3
  • 11
1
vote
1 answer

Php preg_replace, replace only a subtring of matched element

With preg_replace, I can replace a matched substring by another like this : echo preg_replace("yellow", "blue", "sky is yellow"); // print "sky is blue" But is it possible to replace only a substring in the search string by another string ? By…
spacecodeur
  • 2,206
  • 7
  • 35
  • 71
1
vote
3 answers

Expand inverted regex to allow 1 space character

How can I alter the pattern below to allow 1 space character ? $name = 'too long name'; $pattern_name = '/[^a-zA-Z]/'; if (preg_match($pattern_name,$name)) { // remove any non-letter characters $name = preg_replace($pattern_name,'',$name); …
Kim
  • 2,747
  • 7
  • 41
  • 50
1
vote
1 answer

Remove all http and https from HTML but exclude placeholder

I want to remove all http: and https: in the HTML files but exclude placeholder="http: and placeholder="https:. I have tried the following example but every http: and https: will be removed: /(?!placeholder=")(http:|https:)/
Thomas Müller
  • 420
  • 4
  • 15
1
vote
2 answers

Regular Expression matching with start tag and closing tag as a new line

I've been producing a letter compilation system (to save people time after a questionaire has been filled in) and near the end of the project we've found a bug. Long story short it would take many hours to fix without this regular expression - which…
Gordi555
  • 25
  • 3
1
vote
4 answers

Remove

tag from HTML content

I trying to get a clean paragraph to be displayed on the website but, because of the

in the middle of the content I am not getting the expected output. I have tried the following but none of them worked and I am still getting

around…
1
vote
1 answer

find reason for automatic encoding detection (UTF-8 vs Windows-1252)

I have a CSV with content that is UTF-8 encoded. However, various applications and systems errorneously detect the encoding of the CSV as Windows-1252, which breaks all the special characters in the file (e.g. Umlauts). I can see that Sublime Text…
fritzmg
  • 2,494
  • 3
  • 21
  • 51
1
vote
1 answer

detect links from sql data and show only 50 letters of it as text

I have been working on a coupon code website and was able to modify some settings, but this one seems to be troubling me... description; …
Shijil
  • 15
  • 3
1
vote
1 answer

Matching all tags in preg_replace

We are new to regex (preg_replace) in PHP and are having a little trouble getting it to do exactly what we want. We have, for example, HTML code like this:

Automatic Writing – A Conduit For An Entity From Another…

Ted Wilmont
  • 463
  • 2
  • 9
  • 20
1 2 3
99
100