Okay so consider the following string:
"Hello How are you??"
I'd like it to return:
"Hello_How_are_you"
But my preg_replace is this:
preg_replace("/[^A-Za-z0-9]/","_",$string);
Which returns the following:
"Hello____How are you"
Whilst good, it gets rid of the foreign characaters but leaves a long line of ___ which looks ugly. I understand why, because it is replacing the spaces with _ which I what I asked it to do. However i'd like to to only output the one _ where it's replaced things.
How can I achiieve this, is it done in the regex or some other way?
Just add a quantifier: +. + means at least one of what was before, grab as many possible:
preg_replace("/[^A-Za-z0-9]+/","_",$string);
This can be an alternative regex.
$str = "Hello How are you??";
$op = preg_replace("/\s+/", "_", $str); // suggested by #Toto
//(or)
$op = preg_replace("/[\s]{1,}/", "_", $str);
Related
Imagine if:
$string = "abcdabcdabcdabcdabcdabcdabcdabcd";
How do I remove the repeated sequence of characters (all characters, not just alphabets) in the string so that the new string would only have "abcd"? Perhaps running a function that returns a new string with removed repetitions.
$new_string = remove_repetitions($string);
The possible string before removing the repetition is always like above. I don’t know how else to explain since English is not my first language. Other examples are,
$string = “EqhabEqhabEqhabEqhabEqhab”;
$string = “o=98guo=98guo=98gu”;
Note that I want it to work with other sequence of characters as well. I tried using Regex but I couldn't figure out a way to accomplish it. I am still new to php and Regex.
For details : https://algorithms.tutorialhorizon.com/remove-duplicates-from-the-string/
In different programming have a different way to remove the same or duplicate character from a string.
Example: In PHP
<?php
$str = "Hello World!";
echo count_chars($str,3);
?>
OutPut : !HWdelor
https://www.w3schools.com/php/func_string_count_chars.asp
Here, if we wish to remove the repeating substrings, I can't think of a way other than knowing what we wish to collect since the patterns seem complicated.
In that case, we could simply use a capturing group and add our desired output in it the remove everything else:
(abcd|Eqhab|guo=98)
I'm guessing it should be simpler way to do this though.
Test
$re = '/.+?(abcd|Eqhab|guo=98)\1.+/m';
$str = 'abcdabcdabcdabcdabcdabcdabcdabcd
EqhabEqhabEqhabEqhabEqhab
o98guo=98guo=98guo=98guo=98guo=98guo=98guo98';
$subst = '$1';
$result = preg_replace($re, $subst, $str);
echo $result;
Demo
You did not tell what exactly to remove. A "sequnece of characters" can be as small as just 1 character.
So this simple regex should work
preg_replace ( '/(.)(?=.*?\1)/g','' 'abcdabcdabcdabcdabcdabcd');
I am using str_replace() to replace all the slashes in a string with '%' and then I run the newly edited string through a function. One of the things this function does is adds '%%%' to the string.
Now, I need to replace only the percentage signs that I replaced before, that were slashes before, with slashes again.
So if the original string looks like this: 'the/wuick/bornw/foc/jumps/over/the/lazy/dog.'
After passing it through the function it will look like this: 'The%quick%brown%fox%jumps%over%the/lazy%dog.%%%'
And then after putting it through the part I need help with, it will look like this: 'The/quick/brown/fox/jumps/over/the/lazy/dog.%%%'
I would greatly appreciate any and all help replacing only the % that I had done with str_replace() before.
To replace single % signs with slashes, you can use
$result = preg_replace('/(?<!%)%(?!%)/', '/', $subject);
This uses negative lookaround assertions to make sure only those % signs are matched that are neither preceded nor followed by another % sign.
See it on regex101.com.
without too many complications execute in this order:
$str = 'The%quick%brown%fox%jumps%over%the/lazy%dog.%%%';
$str= str_replace('%%%','***triple_percent**', $str);
$str= str_replace('%','/', $str);
$str= str_replace('***triple_percent**','%%%', $str);
Idealy first see why you have so many %, I am sure you can simplify your functions.
Another solution is using regular expressions like Tim says in his answer
$str= preg_replace('/(?<!%)%(?!%)/', '/', $str);
Break apart means:
(?<!%) Not % before
% find %
(?!%) Not % after
also add g so it can find it many times, and i for case sensitive in the case that you might need it:
$str= preg_replace('/(?<!%)%(?!%)/ig', '/', $str);
Add another replate with blank string like:
$string = str_replace('%', '', $string);
So I've seen a couple articles that go a little too deep, so I'm not sure what to remove from the regex statements they make.
I've basically got this
foo:bar all the way to anotherfoo:bar;seg98y34g.?sdebvw h segvu (anything goes really)
I need a PHP regex to remove EVERYTHING after the colon. the first part can be any length (but it never contains a colon. so in both cases above I'd end up with
foo and anotherfoo
after doing something like this horrendous example of psuedo-code
$string = 'foo:bar';
$newstring = regex_to_remove_everything_after_":"($string);
EDIT
after posting this, would an explode() work reliably enough? Something like
$pieces = explode(':', 'foo:bar')
$newstring = $pieces[0];
explode would do what you're asking for, but you can make it one step by using current.
$beforeColon = current(explode(':', $string));
I would not use a regex here (that involves some work behind the scenes for a relatively simple action), nor would I use strpos with substr (as that would, effectively, be traversing the string twice). Most importantly, this provides the person who reads the code with an immediate, "Ah, yes, that is what the author is trying to do!" instead of, "Wait, what is happening again?"
The only exception to that is if you happen to know that the string is excessively long: I would not explode a 1 Gb file. Instead:
$beforeColon = substr($string, 0, strpos($string,':'));
I also feel substr isn't quite as easy to read: in current(explode you can see the delimiter immediately with no extra function calls and there is only one incident of the variable (which makes it less prone to human errors). Basically I read current(explode as "I am taking the first incident of anything prior to this string" as opposed to substr, which is "I am getting a substring starting at the 0 position and continuing until this string."
Your explode solution does the trick. If you really want to use regexes for some reason, you could simply do this:
$newstring = preg_replace("/(.*?):(.*)/", "$1", $string);
A bit more succinct than other examples:
current(explode(':', $string));
You can use RegEx that m.buettner wrote, but his example returns everything BEFORE ':', if you want everything after ':' just use $2 instead of $1:
$newstring = preg_replace("/(.*?):(.*)/", "$2", $string);
You could use something like the following. demo: http://codepad.org/bUXKN4el
<?php
$s = 'anotherfoo:bar;seg98y34g.?sdebvw h segvu';
$result = array_shift(explode(':', $s));
echo $result;
?>
Why do you want to use a regex?
list($beforeColon) = explode(':', $string);
I am attempting to remove some line breaks and spaces from a multiline string I have, such as the following:
Toronto (YTZ)
to
Montreal (YUL)
I tried doing:
$matched = preg_replace('/[\n]/', '', $string);
var_dump($matched);
but all it returns is:
Montreal (YUL)
I've tried all sorts of combinations of regular expressions, but it only ever seems to find what I specify, replace it, and display anything AFTER the matched expression.
I'm sure it's something simple, but I can't seem to figure it out.
Thanks in advance!
\n only represents "go to line" if it is between double quotes in PHP "\n"... Your regex should be "/[\n]/" not '/[\n]/'
Anyway, don't use a regular expression for that, but str_replace("\n",'',$string) instead. It's faster.
As Kash already noticed you, expression of new line in different OS can be different.
That's where PHP_EOL constant is used. This constant is defined depending on OS.
$string = str_replace(PHP_EOL, '', $string);
if string could be created on different machine, then it would be better to replace "\r" and "\n" separately
$string = str_replace(array("\r", "\n"), '', $string);
$str = preg_replace('/\n+(?=.)/', " ",
preg_replace('/^\s*/m', "",
$str));
Check this code here.
I'm attempting to remove noise words from a string, and I have what I believe is a good algorithm for it, but I'm running into a snag. Before I do my preg_replace I remove all punctuation except apostrophe ('). The I put it through this preg_replace:
$content = preg_replace('/\b('.implode('|', self::$noiseWords).')\b/','',$content);
Which works great, except for words that do indeed have that ' character. preg_replace seems to be treating that as a boundary character. This is a problem for me.
Is there a way I can get around this? A different solution perhaps?
Thanks!
Here is the example I'm using:
$content = strtolower(strip_tags($content));
$content = preg_replace("/(?!['])\p{P}/u", "", $content);// remove punctuation
echo $content;// i've added striptags for editing as well should still workyep it doesnbsp
$content = preg_replace("/\b(?<')(".implode('|', self::$noiseWords).")(?!')\b/",'',$content);
$contentArray = explode(" ", $content);
print_r($contentArray);
On the 3rd line you'll see the comment of what $content is right before the preg_replace
And though I'm assuming you can guess what my noiseWords array looks like, here's just a small fraction of it:
$noiseWords = array("a", "able","about","above","abroad","according","accordingly","across",
"actually","adj","after","afterwards","again",......)
You can use a negative lookbehind and positive lookahead to make sure you're not "around" a quote character:
$regex = "/\b(?<!')(".implode('|', self::$noiseWords).")(?!')\b/";
Now, your regex will not match anything that is preceded by or following with a single quote.