Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a set of strings that are in the format of This.is.An.Example.YYYY.More.Data.Here where YYYY is any valid year and I would like to get the string up until YYYY in the example so I would end up with a string that is This.is.an.Example.
You can do this with a simple regex, \d{4}.
$splitted = preg_split('~\d{4}~', 'This.is.An.Example.2015.More.Data');
echo $splitted[0];
Output:
This.is.An.Example.
This will split one every 4 continuous single numbers (0-9). Since you only care about what is the first instance use the 0 index.
Sample: http://sandbox.onlinephpfunctions.com/code/640224e9bda3e6cd5c3e012a1bb901dc1e6a34b4
Vague question, so I choose the simplest version ;-)
<?php
$in = 'This.is.An.Example.2015.More.Data.Here';
$out = strstr($in, '.2015', true);
echo $out;
prints This.is.An.Example
see http://docs.php.net/strstr
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a string for example 'AA231CS'and I want to add a hyphen or dash so it looks like this (the results should be this) 'AA-231-CS'.
and another string '9999ZZZ99' and I want to add a hyphen or dash so it looks like this (the results should be this) '9999-ZZZ-99'.
what's the best approach to handle this?
thank you!
Here is regex required and implode
$string = "9999ZZZ99AAA";
// I have separated string with continous numbers and alphabets in group
preg_match_all('/([0-9]+|[a-zA-Z]+)/',$string,$matches);
// imploding them by `-`
echo implode("-", $matches[0]);
Demo.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have possible string combinations:
$string1 = '[variation-price] for product one.';
$string2 = 'Product two consists of [variation-parts]';
$string3 = 'Simple product';
$replace = 'Contains variations';
I need to check every string if it contains [variation- and if it does, replace the whole word (f.x. [variation-price]) with $replace.
I have tried various functions, bet the problem is that I do not know where exactly in a string variation may appear or how long it will be.
Any help or guidance is much appreciated.
Use \[variation-.*?\] regex with preg_replace():
preg_replace("/\[variation-.*?\]/", $replace, $string1)
Demo: https://ideone.com/MtNZ3L
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I tried several times to find a solution without an idea of how to approach it.
I want to convert this string: '^3816[4-6].*$';
Into this: '38164, 38165, 38166';
In other words I want to genarate all the possible options of a given string: 'XXXX[2-7]';
So the output should be: 'XXXX2, XXXX3, XXXX4, XXXX5, XXXX6, XXXX7';
please help me to solve this issue if anyone know how to accomplish it.
Use a RegExp ;)
<?php
$string_pattern = '^3816[4-6].*$';
$pattern = '/^\^(.*)\[(.)-(.)\](.*)\$$/';
preg_match($pattern, $string_pattern, $matches);
for ($i = $matches[2]; $i <=$matches[3] ; $i++) echo $matches[1].$i.$matches[4]."\n";
?>
The question is: what really contains $matches[4]?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
say I have few String's like:
[0]Hey how are you?
[1]13A315Bdas
[2]d3s315
[3]Billy
[4]daa2315Fasd
How can I use regex in PHP to roll out number 1, 2 and 4 as invalid strings and keep 0 and 3 as valid strings?
So something like:
LettersNumbersLetters = false;
NumbersLetters = false;
Numbers = true;
Letters = true;
So either just Numbers in String is valid, or just letters in String is valid.
You can use (?<=^\[)[035-9](?=\]) regex and if a string contains a match, it is valid. See demo on regex101.com.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have this long number 2241574459 , how can i convert this to string or readable number ?Is there any php functions ?
I know exactly how to accomplish this common task:
$variable = preg_replace(
"/\d{5,}/",
"string or readable number",
$variable
);
This will replace all 5+ digit numbers with string or readable number.
Moral of the story: Make your question clear -- how can anyone know what that integer represents? This is actually the only valid answer to it, as stupid as it is.
$text="2241574459";
$zahl=(int) $text;
Unless I misunderstand your question.