5,832 questions
1
vote
1
answer
87
views
php dynamic replacement with a std object
How can I make a dynamic replacement with a std object?
I can't get how to use $1 in this case :(
See below.
$lang->custom_name = "Me";
$lang->custom_email = "Me@me";
$html =...
1
vote
1
answer
235
views
Get string between first separator and last dot
Sorry for this, I am a very old developer. After have been stopped developing 6 year ago due to a serious cerebral infarction my head is not so fresh anymore, I forgot many thing and also, as you can ...
-2
votes
1
answer
199
views
Regex replace hyphens in text excluding urls, tags and mails
I am trying to replace hyphens in text to non-breaking hyphens, but I need to exlude all URLs, emails and tags. Here is some text I am trying to edit:
Some text with a link but also plain URL like
...
0
votes
1
answer
57
views
Preg_Replace distorting images
I'm trying to do a preg_replace to change the title of my pages since everything is below the header and title tags. I used this example https://stackoverflow.com/a/32508962 and edited it slightly for ...
2
votes
1
answer
30
views
How to give space in sentence in string with preg_replace?
Here's i have an issue :
sentence : "12223233 sekamar berempat"
$kamar = $_POST['kamarpilihan'];
$kamarhuruf = preg_replace("/[^a-z]+""/", "", $kamar);
result:
sekamarberempat
...
2
votes
1
answer
103
views
Insert character before and after of character but ignore space between two characters
How can I insert the character before and after in a character but ignore whitespace between two character or more.
I try this regex but it's return incorrectly
$str = " echo '<pre>';...
-1
votes
3
answers
388
views
Using preg_replace to replace string inside
<?php
$str = htmlspecialchars('<nav><a>something</a></nav>');
$pattern = htmlspecialchars('/<nav><a>something</a></nav>/i');
echo htmlspecialchars(...
0
votes
0
answers
262
views
How can I use PHP preg_match to capture string between and ignore all the outside of the two brackets? [duplicate]
I have an example:
some text lorem ipsum ... {gallery}../2021/some_string_2021/some_other_string{/gallery} ... other lorem ipsum
And I need to get out only the ../2021/some_string_2021/...
0
votes
1
answer
894
views
Wordpress preg_replace ' ' tags in post_content with insert_post_data not working
I have tags as spaces in autocreated posts in Wordpress and trying to replace them before posting/saving. Text sample below:
This is an Example of&...
1
vote
4
answers
266
views
Using preg_replace to reformat money amounts in text with PHP
I'm struggling with some regular expressions. What I want to do is find money amounts in a string, remove the €,$, or £ but keep the number, and then look to see if there is a 'b' or an 'm' - in which ...
-1
votes
1
answer
94
views
Remove first zero from billing phone number after registration [closed]
When registrants sign up, they often add a leading 0. I would like this to be removed after registration. I have tried the below, but it's not working (cq it's not updating the proposed outcome). ...
0
votes
1
answer
449
views
When using preg_replace only use $1 (in the replacement) if $2 doesn't exist
I am currently using this snippet to change the text from [[foo]]to a full HTML a link.
$var = preg_replace('/\[\[((.*?)(\|(.*?))?)\]\]/', '<a href="https://foobar.com/$1">$1</a>'...
-1
votes
1
answer
471
views
PHP preg_replace() returns empty string with hidden control chars
I'm trying to strip hidden control chars (especially \x{89} and \x{88}) with preg_replace() from a string. It is "ˆText" (it starts with an "\x{88}" char), mb_detect_encoding says ...
0
votes
3
answers
511
views
preg_replace not working on groups of symbols
So I am trying to make a morse code encoder/decoder; I got the encoder done, but the decoder is giving me some problems.
So, if I use the function test and input "ab" it will return "ab&...
1
vote
1
answer
947
views
Is there any way to add a starting of link to a unknown m3u8 file
I recently wrote a code which uses file_get_contents along with some USER_AGENT headers and other headers to get a source of a m3u8 file , when echoed it looks like following
#EXTM3U #EXT-X-STREAM-INF:...
1
vote
2
answers
123
views
How to replace in PHP all matches of a digit plus a space with comma in a string
$str = "Hello 1234567 Stack 56789 Overflow 12345";
$str = preg_replace('/([0-9] )/', ',', $str);
I want this "Hello 1234567, Stack 56789, Overflow 12345,..."
2
votes
1
answer
89
views
preg_replace match exact word
I'm trying to replace words in PHP using preg_replace. When executing the code, it will replace "fuel cap for car" with "cap", as opposed to "auto parts".
$string = "...
2
votes
2
answers
331
views
preg_replace replace line breaks across multiple lines between two symbols
How can I match what's between a . and a { and merge them onto a single line without touching the spacing between them.
This regex (\.|@)[^}]*\{ inculdes the ending { no matter what I try, and I need ...
2
votes
2
answers
1k
views
regex to match line breaks and tabs between brackets
I'm trying to remove line breaks and tabs from content that's inside brackets from data like this:
settings:a [
a:[
a:a
b:b
]
b:[
a:a
b:b
]
]
settings:...
-1
votes
1
answer
50
views
preg_replace and preg_match return no results, even though the regular expression is valid [duplicate]
<?php
$teststring = <<<STR
aqbrakadabra
doom
<style>
<script>
sdgdfsf
<?php
<?
dfsaasdf
<? phpinfo(); ?>
STR;
print preg_replace("/\<\?(php){0,1}|(sys)[a-...
1
vote
1
answer
89
views
Removing text with regular expression in PHP
Variable string contains lines as
important 1
some words ......
IMPORTANT 34
some words ......
important 99
some words ......
The goal is to remove the lines containing the word important ignoring ...
0
votes
1
answer
726
views
How to append embed code to youtube anchor link
I use this code to make urls clickable as anchor links in forum posts.
function makelink($str) {
$str = preg_replace_callback('/((((http)(s)?:\/\/)|www\.)[-0-9æøåa-zA-Z?-??-?\(\)%_+\.~#?&;:@\/...
0
votes
1
answer
1k
views
This "preg_replace" not work for me in php 7.4
$regex = array(
"`^([\t\s]+)`ism"=>'',
"`^\/\*(.+?)\*\/`ism"=>"",
"`([\n\A;]+)\/\*(.+?)\*\/`ism"=>"...
0
votes
1
answer
111
views
Simple Dom Parser Returns Empty via Ajax
I'm using Simple HTML Dom Parser to correct some links in my output to good effect but have found some strange behaviour when calling content via Ajax. I'm using the parser on a WordPress site so feed ...
0
votes
1
answer
39
views
Regular Expression for replace all non digit expect symbols
i can't figure out this thing i think it possible with only one pattern, please help me improve.
I have this string 2 / 3 items and i wont receive only 2 / 3
Items can also be write in cirillic so 2 / ...
3
votes
3
answers
244
views
How can I combine two (or more) preg_replace's - PHP
< EDIT >
I Wanted to give a big thank you to all of you who have provided an answer, You have all given me great options. I will play with each and see which one suits my exact needs prior to ...
1
vote
2
answers
49
views
Problem with preg_replace in php - Transform specific codes in html
I am trying to transform this:
§f§lWORDHERE
in this:
<span style="color: white;><b>WORDHERE</b></span>
I thought that with this I would achieve it, but apparently it is ...
0
votes
1
answer
36
views
php preg_replace not putting dash back in?
So, I'm doing some manipulation on lat/long pairs, and I need to turn this:
39.1889375383777,-94.48019109594397
into:
39.1889375383777 -94.48019109594397
I can't use str_replace, unless I want to ...
2
votes
2
answers
95
views
Dynamically replace part of a condition block with regex
There is a string in format:
else if($rule=='somerule1')
echo '{"s":1,"n":"name surname"}';
else if($rule=='somerule2')
echo '{"s":1,"n":"another ...
0
votes
1
answer
401
views
Capture Group Not Captured in Preg_replace Replacement
I have the following code:
$f = 'IMG_1474.PNG';
preg_replace('/(.*)([.][^.]+$)/', '$1' . time() . '$2', $f)
which should split the IMG_1474 and .PNG and add the epoch in between. The first capture ...
0
votes
1
answer
390
views
preg_replace keep alphanumeric + Latin + emojis
I need to filter out all, but alphanumeric, Latin-Chars AND emojis
$str="Hello José' [](){}✅🎧🎬., welcome";
wanted result:
Hello José ✅🎧🎬 welcome
echo preg_replace("/[^\p{Latin} \wp-...
0
votes
0
answers
23
views
How to optimize lowercase and capitalize formats extensions? [duplicate]
I have this code and works correctly but I'm wondering if it can be optimized to not repeat the formats in capitalize & lowercase (?:mp4|MP4|webm|WEBM|mov|MOV|ogg|OGG))
$post[message] = ...
-1
votes
1
answer
44
views
How to reduce this 5 preg_replaces lines in 1 line?
I belive minimal is better, so I'm wondering how could I reduce/optimize these 5 lines in 1 line?
#JPG
$post[message] = preg_replace('/<a href="(.+?)\.jpg" target="_blank">(.+...
1
vote
1
answer
100
views
Use preg_replace to replace a href value and prevent duplicate question mark
I have a code like this :
<?php
add_filter('the_content', 'my_url_replacer');
function my_url_replacer($content) {
$params = $_SERVER['QUERY_STRING'];
if($params) {
return ...
0
votes
1
answer
165
views
regex japanese words cannot escape
I would like to remove some hashtag including japanese and I encounter a problem that some words are not able to be removed.
$text = "#食への好きな人と繋かりたい #食への好きな人と繋がりたい #食べるの好きな人と繋がりたい";
echo ...
0
votes
1
answer
91
views
Regex replace keyword with different string by loop (PHP)
Let say I have a string
$content = "hello . wow . cool . yes!";
It's easy to just replace the dot to another string:
echo preg_replace("/\./", "<img>", $content);
...
0
votes
1
answer
290
views
how can i append utm_source and utm_medium in all url from blog content
Assume I have the below text in the database.
$str = 'Test text http://hello-world.com Test text http://google.com/file.jpg?google=1 Test text https://hell.o.wor.ld/test?qwe=qwe Test text text text ...
1
vote
1
answer
927
views
PHP Remove extra spaces between break lines
I have a string in PHP, i'm able to remove multiple continuous break lines and multiple spaces, but what i'm still not able is to remove multiple break lines if i have an space in the middle.
For ...
-1
votes
1
answer
354
views
regex matching content that's between angle brackets [duplicate]
How can I convert all multiple white space inside any given HTML element to a single space using regex and preg_replace in php?
Eg: <div class="myClass" jsaction="UjQMac:.CLIENT&...
1
vote
2
answers
523
views
PHP - Replace specific word inside tag span
I would like to replace the word "custom" with
<span class="persProd">custom</span>.
This is my code but not work:
$output = '<a href="www.mysite.com/custom-...
1
vote
3
answers
64
views
With regular expression, transform separated words into a reference
With PHP, I'm trying to use preg_replace to convert a group of words separated with an underline into a "reference" composed of the first letter of each word except for the last one in the ...
0
votes
0
answers
100
views
Difference problem in finding word count in PHP and JS [duplicate]
There are 2 codes, PHP and JS. The work of the codes is exactly the same.
The duty of the codes is to count words. However, there is 1 difference between PHP and JS results.
The reason for this ...
1
vote
2
answers
68
views
Make this regex code apply to words with an apostrophe
I use this code to select 2 words around either side of a word.
((\w+\W+){0,2}WORDHERE(\W+\w+){0,2})
But it treats apostrophe-separated words as "two words".
For example, with the input ...
-3
votes
2
answers
39
views
How to remove a characters in string php [closed]
I am stuck with this. I tried json_decode but it gives me an error.
How can I remove the "[]" and the ' " " ' characters of my string?
I have a string with this values ["Red ...
1
vote
1
answer
549
views
PHP Regex to remove HTML-Tags inside <pre></pre> code blocks
I have a tricky string of HTML code that includes several pre tags that inside them include code (say, python), and that are also decorated by HTML tags that should be removed.
For example:
Some text.
...
0
votes
1
answer
1k
views
Remove JSON comments with a regular expression except the one inside strings
I want to remove all comment inside a JSON object except the one that are inside a string.
For example:
{
//Remove this comment
"Command": "storeSystemConfig",
"...
0
votes
1
answer
92
views
PHP Regex - How to do str_replace on preg_replace numeric backreferences?
I had the following PHP Regex code, to replace all emails with a "[removed email]" replacement string; and it worked nicely:
$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement =...
0
votes
1
answer
37
views
preg_replace inside mysql while cannot be printed outside the loop php [duplicate]
I am trying to print a preg_replace result outside the loop it is made.
This is my code
$search = "Lola and Chris";
$query_gdpr_names = "select name FROM gdpr_names";
$...
1
vote
3
answers
142
views
splitting long regex to pieces PHP
I have a very long list of names and I am using preg_replace to match if a name from the list is anywhere in the string. If I test it with few names in the regex it works fine, but having in mind that ...
2
votes
2
answers
663
views
Replace odd positions in string with a random digit in php
I have a string from which I want to replace only the odd positions by a random digit.
For Example, the string is '123456'. Now the output I want is '528496';
Note that the digits 1,3,5 in odd ...