Example
Input = 1.1.0.1
Expected output = 1.101
You could make use substr() and str_replace() fairly easily:
$str = '1.1.0.1';
$pos = strpos($str,'.');
if ($pos !== false) {
$str = substr($str,0,$pos+1) . str_replace('.','',substr($str,$pos+1));
}
echo $str;
$s = preg_replace('/((?<=\.)[^.]*)\./', '$1', $s);
Matches zero or more non-dot characters followed by a dot, but only if the match was preceded by a dot. This prevents a match on the initial digit(s). Replaces the match with only the non-dot characters (the digits), which were captured in group #1.
$input="1.1.1.1";
$s = explode(".",$input ) ;
$t=array_slice($s, 1);
print implode(".",array($s[0] , implode("",$t)) );
or
$input="1.1.1.1";
$s = explode(".",$input ,2) ;
$s[1]=str_replace(".","",$s[1]);
print implode(".",array($s[0] ,$s[1] ) );
Match&Release the first occurring literal dot
Replace all subsequent literal dots
Code: (Demo)
echo preg_replace('~^[^.]*\.(*SKIP)(*FAIL)|\.~', '', $string);
// 1.101
Or with the "continue" character (\G), consume and forget the first literal dot, then replace all subsequent literal dots.
Code: (Demo)
echo preg_replace('~(?:^[^.]*\.|\G(?!^))[^.]*\K\.~', '', $string);
// 1.101
Or simply check that a literal dot has a literal dot occurring earlier in the string.
Code: (Demo)
echo preg_replace('~(?<=\.)[^.]*\K\.~', '', $string);
// 1.101
I though substr_replace() would work here, but sadly no... Here is a regex approach:
$str = preg_replace('~(\d+\.)(\d+)\.(\d+)\.(\d+)~', '$1$2$3$4', $str);
You could also try the below regex with s switch,
<?php
$string = '1.1.0.1';
$pattern = "/(?s)((?<=\.).*?)(\.)/i";
$replacement = "$1";
echo preg_replace($pattern, $replacement, $string);
?>
Output:
1.101
Using regex matches can be clearer by depicting the desired result and avoids the error-prone approach of calls to substr and strpos. Here I assume that no text is required before the first dot, i.e., that an input may begin with a dot that must be preserved. The difference is whether a quantifier of * or + is appropriate in the patterns below.
If your inputs will always be short, a straightforward approach is to replace trailing dots until none remain:
$count = 0;
$output = $input;
do {
$output = preg_replace('/^(.*\.)(.+)\./', '$1$2', $output, -1, $count);
} while ($count != 0);
echo $output;
To do it with a single regex match, use preg_replace_callback to apply a function (str_replace in this case) to the backreference variable $2.
$output = preg_replace_callback(
'/^([^.]*\.)(.+)$/',
function ($m) { return $m[1] . str_replace('.', '', $m[2]); },
$input);
Sample results:
1.1.0.1 - 1.101
.1.0.1 - .101
111 - 111
1.1 - 1.1
1. - 1.
.1 - .1
.1. - .1
.... - .
You may want to experiment with the code and test cases at Try It Online!
Related
I need to know how to do this reliably with the least amount of calls.
I have a string and I need to remove the 8th character from it. It doesn't matter what the char is, I just need to remove ONLY the 8th char.
I came up with this but a little too unwieldy for me.
// 12345678901234567890
$str = '5NMSG3AB1AH353158';
// after removing char, result should be:
// 5NMSG3A1AH353158
$r = str_split($str);
unset($r[7]);
echo join('', $r);
Possibly Regex?
Here are some solutions:
$str = substr($str, 0, 7) . substr($str, 8);
$str = substr_replace($str, '', 7, 1);
$str = preg_replace('/(.{7})./', '$1', $str, 1);
$str = preg_replace('/.{7}\K./', '', $str, 1);
I'd go for substr() or better substr_replace() as this will certainly be fastest and most easy to read.
substr_replace makes this very simple.
$string = substr_replace($string, '', 7, 1);
It can also take an array of strings as its first parameter, and do the same replacement in all of them, which can be pretty handy.
Like this
$str = '5NMSG3AB1AH353158';
echo preg_replace('/^(.{7})./', '\1', $str);
Output:
5NMSG3A1AH353158
Sandbox
Explanation
^ start of string
(...) capture
. match any
{7} seven times
. match any one time
Then the replacement
\1 first capture group
Basically capture the first 7 chars, then match the 8th, and replace that with the captured 7 chars. Which effectively removes the 8th.
UPDATE
here is another way I like (I haven't used that function sense college, I think):
$s = '5NMSG3A1AH353158';
echo substr_replace($s,'',7,1); //strings are 0 based
sandbox
substr_replace() replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement.
mixed substr_replace( mixed $string, mixed $replacement, mixed $start [, mixed $length ] )
http://php.net/manual/en/function.substr-replace.php
Simple.
I have the following string:
$thetextstring = "jjfnj 948"
At the end I want to have:
echo $thetextstring; // should print jjf-nj948
So basically what am trying to do is to join the separated string then separate the first 3 letters with a -.
So far I have
$string = trim(preg_replace('/s+/', ' ', $thetextstring));
$result = explode(" ", $thetextstring);
$newstring = implode('', $result);
print_r($newstring);
I have been able to join the words, but how do I add the separator after the first 3 letters?
Use a regex with preg_replace function, this would be a one-liner:
^.{3}\K([^\s]*) *
Breakdown:
^ # Assert start of string
.{3} # Match 3 characters
\K # Reset match
([^\s]*) * # Capture everything up to space character(s) then try to match them
PHP code:
echo preg_replace('~^.{3}\K([^\s]*) *~', '-$1', 'jjfnj 948');
PHP live demo
Without knowing more about how your strings can vary, this is working solution for your task:
Pattern:
~([a-z]{2}) ~ // 2 letters (contained in capture group1) followed by a space
Replace:
-$1
Demo Link
Code: (Demo)
$thetextstring = "jjfnj 948";
echo preg_replace('~([a-z]{2}) ~','-$1',$thetextstring);
Output:
jjf-nj948
Note this pattern can easily be expanded to include characters beyond lowercase letters that precede the space. ~(\S{2}) ~
You can use str_replace to remove the unwanted space:
$newString = str_replace(' ', '', $thetextstring);
$newString:
jjfnj948
And then preg_replace to put in the dash:
$final = preg_replace('/^([a-z]{3})/', '\1-', $newString);
The meaning of this regex instruction is:
from the beginning of the line: ^
capture three a-z characters: ([a-z]{3})
replace this match with itself followed by a dash: \1-
$final:
jjf-nj948
$thetextstring = "jjfnj 948";
// replace all spaces with nothing
$thetextstring = str_replace(" ", "", $thetextstring);
// insert a dash after the third character
$thetextstring = substr_replace($thetextstring, "-", 3, 0);
echo $thetextstring;
This gives the requested jjf-nj948
You proceeding is correct. For the last step, which consists in inserting a - after the third character, you can use the substr_replace function as follows:
$thetextstring = 'jjfnj 948';
$string = trim(preg_replace('/\s+/', ' ', $thetextstring));
$result = explode(' ', $thetextstring);
$newstring = substr_replace(implode('', $result), '-', 3, false);
If you are confident enough that your string will always have the same format (characters followed by a whitespace followed by numbers), you can also reduce your computations and simplify your code as follows:
$thetextstring = 'jjfnj 948';
$newstring = substr_replace(str_replace(' ', '', $thetextstring), '-', 3, false);
Visit this link for a working demo.
Oldschool without regex
$test = "jjfnj 948";
$test = str_replace(" ", "", $test); // strip all spaces from string
echo substr($test, 0, 3)."-".substr($test, 3); // isolate first three chars, add hyphen, and concat all characters after the first three
I have some string, for example:
cats, e.g. Barsik, are funny. And it is true. So,
And I want to get as result:
cats, e.g. Barsik, are funny.
My try:
mb_ereg_search_init($text, '((?!e\.g\.).)*\.[^\.]');
$match = mb_ereg_search_pos();
But it gets position of second dot (after word "true").
How to get desired result?
Since a naive approach works for you, I am posting an answer. However, please note that detecting a sentence end is a very difficult task for a regex, and although it is possible to some degree, an NLP package should be used for that.
Having said that, I suggested using
'~(?<!\be\.g)\.(?=\s+\p{Lu})~ui'
The regex matches any dot (\.) that is not preceded with a whole word e.g (see the negative lookbehind (?<!\be\.g)), but that is followed with 1 or more whitespaces (\s+) followed with 1 uppercase Unicode letter \p{Lu}.
See the regex demo
The case insensitive i modifier does not impact what \p{Lu} matches.
The ~u modifier is required since you are working with Unicode texts (like Russian).
To get the index of the first occurrence, use a preg_match function with the PREG_OFFSET_CAPTURE flag. Here is a bit simplified regex you supplied in the comments:
preg_match('~(?<!т\.н)(?<!т\.к)(?<!e\.g)\.(?=\s+\p{L})~iu', $text, $match, PREG_OFFSET_CAPTURE);
See the lookaheads are executed one by one, and at the same location in string, thus, you do not have to additionally group them inside a positive lookahead. See the regex demo.
IDEONE demo:
$re = '~(?<!т\.н)(?<!т\.к)(?<!e\.g)\.(?=\s+\p{L})~iu';
$str = "cats, e.g. Barsik, are funny. And it is true. So,";
preg_match($re, $str, $match, PREG_OFFSET_CAPTURE);
echo $match[0][1];
Here are two approaches to get substring from start to second last . position of the initial string:
using strrpos and substr functions:
$str = 'cats, e.g. Barsik, and e.g. Lusya are funny. And it is true. So,';
$len = strlen($str);
$str = substr($str, 0, (strrpos($str, '.', strrpos($str, '.') - $len - 1) - $len) + 1);
print_r($str); // "cats, e.g. Barsik, and e.g. Lusya are funny."
using array_reverse, str_split and array_search functions:
$str = 'cats, e.g. Barsik, and e.g. Lusya are funny. And it is true. So,';
$parts = array_reverse(str_split($str));
$pos = array_search('.', $parts) + 1;
$str = implode("", array_reverse(array_slice($parts, array_search('.', array_slice($parts, $pos)) + $pos)));
print_r($str); // "cats, e.g. Barsik, and e.g. Lusya are funny."
I am trying to something like this.
Hiding users except for first 3 characters.
EX)
apple -> app**
google -> goo***
abc12345 ->abc*****
I am currently using php like this:
$string = "abcd1234";
$regex = '/(?<=^(.{3}))(.*)$/';
$replacement = '*';
$changed = preg_replace($regex,$replacement,$string);
echo $changed;
and the result be like:
abc*
But I want to make a replacement to every single character except for first 3 - like:
abc*****
How should I do?
Don't use regex, use substr_replace:
$var = "abcdef";
$charToKeep = 3;
echo strlen($var) > $charToKeep ? substr_replace($var, str_repeat ( '*' , strlen($var) - $charToKeep), $charToKeep) : $var;
Keep in mind that regex are good for matching patterns in string, but there is a lot of functions already designed for string manipulation.
Will output:
abc***
Try this function. You can specify how much chars should be visible and which character should be used as mask:
$string = "abcd1234";
echo hideCharacters($string, 3, "*");
function hideCharacters($string, $visibleCharactersCount, $mask)
{
if(strlen($string) < $visibleCharactersCount)
return $string;
$part = substr($string, 0, $visibleCharactersCount);
return str_pad($part, strlen($string), $mask, STR_PAD_RIGHT);
}
Output:
abc*****
Your regex matches all symbols after the first 3, thus, you replace them with a one hard-coded *.
You can use
'~(^.{3}|(?!^)\G)\K.~'
And replace with *. See the regex demo
This regex matches the first 3 characters (with ^.{3}) or the end of the previous successful match or start of the string (with (?!^)\G), and then omits the characters matched from the match value (with \K) and matches any character but a newline with ..
See IDEONE demo
$re = '~(^.{3}|(?!^)\G)\K.~';
$strs = array("aa","apple", "google", "abc12345", "asdddd");
foreach ($strs as $s) {
$result = preg_replace($re, "*", $s);
echo $result . PHP_EOL;
}
Another possible solution is to concatenate the first three characters with a string of * repeated the correct number of times:
$text = substr($string, 0, 3).str_repeat('*', max(0, strlen($string) - 3));
The usage of max() is needed to avoid str_repeat() issue a warning when it receives a negative argument. This situation happens when the length of $string is less than 3.
I am running into a problem trying to do a replacement on a few strings. Essentially what I have is a bunch of prices on my page that look like
RMB148.00
What i am trying to do is run a replace on only the last 2 numbers so i can do something like
RMB14800
Preg replace works fine for the RMB part because it is always there.
My problem is the last two numbers can be anything it all depends on the price so I cant just remove and replace, I need to just wrap HTML <sup> tags around them.
$string = $product['price'];
$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
echo preg_replace('/RMB/', '<sup class="currency-sym">RMB</sup>', $string, 1);
Assuming the last two characters are digits, you could just
$string=preg_replace('/(\d\d)$/', '<sup class="currency-sym">\1</sup>', $string);
If not,
$string=preg_replace('/(..)$/', '<sup class="currency-sym">\1</sup>', $string);
should do the trick.
Alternativly use
$string=substr($string,0,-2).'<sup class="currency-sym">'.substr($string,-2).'</sup>';
Here is a regex solution that looks for the final digit notation at the end of your string.
$string = 'RMB148.00';
$string = preg_replace('/(\d+)\.(\d{2})\z/','$1<sup>$2</sup>',$string);
echo $string;
You could use the following with the explode () function
$string = explode ('.', $product['price']);
$new_string = $string[0].'<sup>'. $string [1]. '</sup>';
And do the regex for the RMB the same way.
Code.
<?php
$string = '14842.00';
$string = substr($string, 0, strlen($string) - 2) . '<sup>' . substr($string, strlen($string) - 2, 2) . '</sup>';
echo $string;
Try online sandbox.
Explanation.
substr($s, $i, $l) gets $l symbols of $s, started from $i index (indexes starts from zero).
So first substr($string, 0, strlen($string) - 2) gets all string except last two symbols.
Second substr($string, strlen($string) - 2, 2) gets only last two symbols.
More about substr.
You should use a pattern matching regex. Note the $1 in the replacement argument matches (\d{2}) in the pattern argument. preg_replace() only replaces the matched pattern. This pattern matches . followed by any two digits. Since . is not included in the replacement argument it does not show up in your $string.
$string = preg_replace('/\.(\d{2})$/', '<sup>$1</sup>', $string);
Of course, you could use one preg_replace to do what you want:
$string = preg_replace('/^(RMB)(\d+)(\.(\d{2}))?$/', "<sup class='currency-sym'>$1</sup>$2<sup>$4</sup>", $string);
The second example may be a good idea if you want DOM integrity, otherwise it creates an empty <sup></sup> when there is no decimal.