PHP Regex - Replace substring within a string - php

If I have the following string for example:
"Value1 *|VALUE_2|* Value3"
I want to be able to remove any substrings from within the string that begins with the chracters | and end in |. I will not know what will be between these characters but this is irrelevant as I want to remove whatever is between them.
Basically, I am just unsure of what pattern to use in the code below.
preg_replace("*|PATTERN|*", "", $str);

| is a special character that needs to be escaped in regex:
preg_replace('/\|[^|]+\|/', '', $str);

preg_replace('/|[^|]+|/', '', $str);
This should do the trick.

preg_replace("/\|.*\|/U", "", $str);
I think this should do the trick..
The /U makes the pattern ungreedy so that in Value1 |VALUE_2| Value3|test the match will be |VALUE_2| instead o |VALUE_2| Value3|
If you only want the contents between the |'s removed, you can do:
preg_replace("/\|.*\|/U", "||", $str);
There are other ways, but this would be the most easy one imho.

Related

Make a multiple delimiter REGEX for preg_split

I need to split multiple lines in multiple files by different delimiters. I think preg_split should do the job but i never worked with PCRE REGEX stuff. I could also change all my delimiters to be consistent but that adds unnecessary calculations.
Q: My delimiters consist of (,)(;)(|)(space) and i am curious how to build such a REGEX.
Put the characters in square brackets []:
$parts = preg_split('/[,;| ]/', $string, null, PREG_SPLIT_NO_EMPTY);
You can also use \s instead of a space character, which matches all kinds of whitspace, such as tabs and newlines.
Try this:
$string = "foo:bar|it;is:simple";
print_r(preg_split ( '/,|;|\||\s/' , $string ));

preg_replace to remove stand-alone numbers

I'm looking to replace all standalone numbers from a string where the number has no adjacent characters (including dashes), example:
Test 3 string 49Test 49test9 9
Should return Test string 49Test 49Test9
So far I've been playing around with:
$str = 'Test 3 string 49Test 49test9 9';
$str= preg_replace('/[^a-z\-]+(\d+)[^a-z\-]+?/isU', ' ', $str);
echo $str;
However with no luck, this returns
Test string 9Test 9test9
leaving out part of the string, i thought to add [0-9] to the matches, but to no avail, what am I missing, seems so simple?
Thanks in advance
Try using a word boundary and negative look-arounds for hyphens, eg
$str = preg_replace('/\b(?<!-)\d+(?!-)\b/', '', $str);
Not that complicated, if you watch the spaces :)
<?php
$str = 'Test 3 string 49Test 49test9 9';
$str = preg_replace('/(\s(\d+)\s|\s(\d+)$|^(\d+)\s)/iU', '', $str);
echo $str;
Try this, I tried to cover your additional requirement to not match on 5-abc
\s*(?<!\B|-)\d+(?!\B|-)\s*
and replace with a single space!
See it here online on Regexr
The problem then is to extend the word boundary with the character -. I achieved this by using negative look arounds and looking for - or \B (not a word boundary)
Additionally I am matching the surrounding whitespace with the \s*, therefore you have to replace with a single space.
I would suggest using
explode(" ",$str)
to get an array of the "words" in your string. Then it should be easier to filter out single numbers.

How to replace one or more consecutive spaces with one single character?

I want to generate the string like SEO friendly URL. I want that multiple blank space to be eliminated, the single space to be replaced by a hyphen (-), then strtolower and no special chars should be allowed.
For that I am currently the code like this:
$string = htmlspecialchars("This Is The String");
$string = strtolower(str_replace(htmlspecialchars((' ', '-', $string)));
The above code will generate multiple hyphens. I want to eliminate that multiple space and replace it with only one space. In short, I am trying to achieve the SEO friendly URL like string. How do I do it?
You can use preg_replace to replace any sequence of whitespace chars with a dash...
$string = preg_replace('/\s+/', '-', $string);
The outer slashes are delimiters for the pattern - they just mark where the pattern starts and ends
\s matches any whitespace character
+ causes the previous element to match 1 or more times. By default, this is 'greedy' so it will eat up as many consecutive matches as it can.
See the manual page on PCRE syntax for more details
echo preg_replace('~(\s+)~', '-', $yourString);
What you want is "slugify" a string. Try a search on SO or google on "php slugify" or "php slug".

How to convert the & character to and using PHP

I was wondering how can I convert user submitted data that contains an & to and using PHP before its stored in the database.
Use str_replace
$after = str_replace('&', 'and', $before);
Note that this will perform a straightforward replacement, hence (as seen on ideone.com):
$text = "ben & jerry, vanilla&coke";
echo str_replace('&', 'and', $text)."\n";
# ben and jerry, vanillaandcoke
If you want to insert spaces when there previously wasn't, then you may want to use regular expressions function like preg_replace as follows:
echo preg_replace('/\s*&\s*/', ' and ', $text)."\n";
# ben and jerry, vanilla and coke
The pattern \s* is the regex pattern for "zero or more" (*) of whitespace characters (\s). In other words, this will replace &, including any preceding and following whitespaces (of any length), and replace it with ' and '.
You could do it like this:
$str = str_replace("&", "and", $str);
Using str_replace (if your data is in a single character encoding) or mb_ereg_replace (preceded by mb_regex_encoding) otherwise.
Note, however, that str_replace is (in this case) safe for UTF-8.

How to replace http:// or www with <a href.. in PHP

I've created this regex
(www|http://)[^ ]+
that match every http://... or www.... but I dont know how to make preg_replace that would work, I've tried
preg_replace('/((www|http://)[^ ]+)/', '\1', $str);
but it doesn't work, the result is empty string.
You need to escape the slashes in the regex because you are using slashes as the delimiter. You could also use another symbol as the delimiter.
// escaped
preg_replace('/((www|http:\/\/)[^ ]+)/', '\1', $str);
// another delimiter, '#'
preg_replace('#((www|http://)[^ ]+)#', '\1', $str);
When using the regex codes provided by the other users, be sure to add the "i" flag to enable case-insensitivity, so it'll work with both HTTP:// and http://. For example, using chaos's code:
preg_replace('!(www|http://[^ ]+)!i', '\1', $str);
First of all, you need to escape—or even better, replace—the delimeters as explained in the other answers.
preg_replace('~((www|http://)[^ ]+)~', '\1', $str);
Secondly, to further improve the regex, the $n replacement reference syntax is preferred over \\n, as stated in the manual.
preg_replace('~((www|http://)[^ ]+)~', '$1', $str);
Thirdly, you are needlessly using capturing parentheses, which only slows things down. Get rid of them. Don't forget to update $1 to $0. In case you are wondering, these are non-capturing parentheses: (?: ).
preg_replace('~(?:www|http://)[^ ]+~', '$0', $str);
Finally, I would replace [^ ]+ with the shorter and more accurate \S, which is the opposite of \s. Note that [^ ]+ does not allow spaces, but accepts newlines and tabs! \S does not.
preg_replace('~(?:www|http://)\S+~', '$0', $str);
Your main problem seems to be that you are putting everything in parentheses, so it doesn't know what "\1" is. Also, you need to escape the "/". So try this:
preg_replace('/(www|http:\/\/[^ ]+)/', '\1', $str);
Edit: It actually seems the parentheses were not an issue, I misread it. The escaping was still an issue as others also pointed out. Either solution should work.
preg_replace('!((?:www|http://)[^ ]+)!', '\1', $str);
When you use / as your pattern delimiter, having / inside your pattern will not work out well. I solved this by using ! as the pattern delimiter, but you could escape your slashes with backslashes instead.
I also didn't see any reason why you were doing two paren captures, so I removed one of them.
Part of the trouble in your situation is that you're running with warnings suppressed; if you had error_reporting(E_ALL) on, you'd have seen the messages PHP is trying to generate about your delimiter problem in your regex.
If there are multiple url contained in a string a separated by a line break instead of a space, you have to use the \S
preg_replace('/((www|http:\/\/)\S+)/', '$1', $val);

Categories