Get the string after a string from a string - php

what's the fastest way to get only the important_stuff part from a string like this:
bla-bla_delimiter_important_stuff
_delimiter_ is always there, but the rest of the string can change.

here:
$arr = explode('delimeter', $initialString);
$important = $arr[1];

$result = end(explode('_delimiter_', 'bla-bla_delimiter_important_stuff'));

I like this method:
$str="bla-bla_delimiter_important_stuff";
$del="_delimiter_";
$pos=strpos($str, $del);
cutting from end of the delimiter to end of string
$important=substr($str, $pos+strlen($del)-1, strlen($str)-1);
note:
1) for substr the string start at '0' whereas for strpos & strlen takes the size of the string (starts at '1')
2) using 1 character delimiter maybe a good idea

$importantStuff = array_pop(explode('_delimiter_', $string));

$string = "bla-bla_delimiter_important_stuff";
list($junk,$important_stufF) = explode("_delimiter_",$string);
echo $important_stuff;
> important_stuff

Related

Remove a string php

I have this value: samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f
Best approach to remove all the string from the beginning of the word until the |. The | is the key here , on where to end. The output should be d76c3c301eb754c62b981f7208158a9f.
Use explode
explode("|","samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f")[1];
check this : https://eval.in/591968
Use stristr()
$str="samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f";
echo ltrim(stristr($str, '|'),"|");
Another short solution using substr and strpos functions:
$str = "samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f";
$result = substr($str, strpos($str, "|") + 1); // contains "d76c3c301eb754c62b981f7208158a9f"
Split your string by using explode(). Then return the last element of the array using end()
end(explode('|', 'samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f'));
Try below code
substr( strstr('samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f', '|'), 1);
Use php explode($delimiter,$yourstring) function
$str="samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f";
$exploded_str_array=explode('|', $str);
echo $required_str=$exploded_str_array[1];
//this contains the second part of the string delimited by |
php manual for the explode function

How to get part of string from the end in PHP?

I am making application where I receive a string from user. The string is concatenated with - character between them. First part of string contains alphabetic data whereas later part contains integers or floating point numbers. For example: A string might be 3 Cups Tea-5.99.I want to get the later part of string 5.99 separated by - character. How to do that? I know about PHP substr() function but that takes fixed characters to retrieve substring from. But in this case the later part will not be fixed. For example: 2 Jeans-65.99. In this case I would need last 4 characters meaning that I can't use substr() function.
Anybody with solution?
I know I would need to apply regex but I am completely novice in Regex.
Waiting for your help.
Thanks!
Simply
$result = explode('-', $string)[1];
For PHP<5.4 you'll have to use temporary variable:
$data = explode('-', $string);
$result = $data[1];
Edit
As mentioned in comments, if there is more than 1 part, that will be:
$result = array_pop(explode('-', $string));
$bits = explode('-', $inputstring);
echo $bits[1];
You can use substr() with strpos():
$str = '3 Cups Tea-5.99';
echo substr($str, strpos($str, "-") + 1);
Output:
5.99
Demo!
If data will be like this: "1-Cup tea-2.99", then
$data = "1-Cup tea-2.99";
$data = explode('-', $string);
$result = $data[count($data)-1];

Insert a slash in a string after every number except at the end (PHP)

i've a string
$string;
in this string i have a number, like 3743687
i wish to add a slash after every number, except at the end, so the result will be
3/7/4/3/6/8/7
is there a php function to do this, right ? any advice ? thanks
$string = '3743687';
$newString = implode('/',str_split($string));
var_dump($newString);
$string = "3743687";
$string = implode("/", str_split($string));
echo $string;

Increment integer at end of string

I have a string, "Chicago-Illinos1" and I want to add one to the end of it, so it would be "Chicago-Illinos2".
Note: it could also be Chicago-Illinos10 and I want it to go to Chicago-Illinos11 so I can't do substr.
Any suggested solutions?
Complex solutions for a really simple problem...
$str = 'Chicago-Illinos1';
echo $str++; //Chicago-Illinos2
If the string ends with a number, it will increment the number (eg: 'abc123'++ = 'abc124').
If the string ends with a letter, the letter will be incremeted (eg: '123abc'++ = '123abd')
Try this
preg_match("/(.*?)(\d+)$/","Chicago-Illinos1",$matches);
$newstring = $matches[1].($matches[2]+1);
(can't try it now but it should work)
$string = 'Chicago-Illinois1';
preg_match('/^([^\d]+)([\d]*?)$/', $string, $match);
$string = $match[1];
$number = $match[2] + 1;
$string .= $number;
Tested, works.
explode could do the job aswell
<?php
$str="Chicago-Illinos1"; //our original string
$temp=explode("Chicago-Illinos",$str); //making an array of it
$str="Chicago-Illinos".($temp[1]+1); //the text and the number+1
?>
I would use a regular expression to get the number at the end of a string (for Java it would be [0-9]+$), increase it (int number = Integer.parse(yourNumberAsString) + 1), and concatenate with Chicago-Illinos (the rest not matched by the regular expression used for finding the number).
You can use preg_match to accomplish this:
$name = 'Chicago-Illinos10';
preg_match('/(.*?)(\d+)$/', $name, $match);
$base = $match[1];
$num = $match[2]+1;
print $base.$num;
The following will output:
Chicago-Illinos11
However, if it's possible, I'd suggest placing another delimiting character between the text and number. For example, if you placed a pipe, you could simply do an explode and grab the second part of the array. It would be much simpler.
$name = 'Chicago-Illinos|1';
$parts = explode('|', $name);
print $parts[0].($parts[1]+1);
If string length is a concern (thus the misspelling of Illinois), you could switch to the state abbreviations. (i.e. Chicago-IL|1)
$str = 'Chicago-Illinos1';
echo ++$str;
http://php.net/manual/en/language.operators.increment.php

PHP: best method to trim a substring from a string

Want to process a set of strings, and trim some ending "myEnding" from the end of each string if it exists.
What is the simplest way to do it?
I know that everything is possible with regexp, but thus seems to be a simple task, and I wonder whether a simpler tool for this exists.
Thanks
Gidi
ima go with preg_replace on this one.
$output = preg_replace('/myEnding$/s', '', $input);
Try this:
$s = "foobarmyEnding";
$toRemove = "myEnding";
$len = strlen($toRemove);
if (strcmp(substr($s, -$len, $len), $toRemove) === 0)
{
$s = substr($s, 0, -$len);
}
ideone
rtrim http://php.net/manual/en/function.rtrim.php
$str = "foobarmyEnding";
$str = rtrim($str, 'myEnding');
// str == "foobar"
ltrim is the same deal for the start of a string http://php.net/manual/en/function.ltrim.php
You could also use str_replace to replace a search term with with an empty string but its slower then rtrim/ltrim if you need to amend the start or end of a string

Categories