0

I want to do this

$name = "hello word XYZabcdefghijklmnopqrstuvwxyz bluesky";

Output should look like: "abcdefghijk"

Which are 11 characters.

Please note: XYZ is the prefix

yivi
  • 42,438
  • 18
  • 116
  • 138
Rubiks
  • 47
  • 7
  • update the code what you tried in question – K.B Dec 27 '17 at 08:56
  • 2
    Possible duplicate of [How to get everything after a certain character?](https://stackoverflow.com/questions/11405493/how-to-get-everything-after-a-certain-character) – yivi Dec 27 '17 at 10:03

2 Answers2

1

The method substr() is what you're looking for:

string substr ( string $string , int $start [, int $length ] )

Returns the portion of string specified by the start and length parameters.

First you find the position of your $prefix in the $name:

strpos($name, $prefix)

Then you need to add the length of your prefix to move to the correct position, and pass the number of characters, which you want to get in the result (11 in your case):

$name = "hello word XYZabcdefghijklmnopqrstuvwxyz bluesky";
$prefix = "XYZ";

$output = substr($name, strpos($name, $prefix) + strlen($prefix), 11);
echo $output;
Dusan
  • 3,284
  • 6
  • 25
  • 46
  • 1
    While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Dec 27 '17 at 10:01
  • You're right. I edited the solution. – Dusan Dec 27 '17 at 12:40
-1

Try this

<?php

$name = "hello word XYZabcdefghijklmnopqrstuvwxyz bluesky";

$out = substr(substr($name, strpos($name, 'XYZ'), 14), 3);

echo $out;
Let's Enkindle
  • 57
  • 2
  • 17