# Display part of string

Hi,

I've got a string like so:
\$str = "2008T(12)-1234P(13)-111T(12)-908P(45)-11T(11)";
I need to if chosen part exists, let's say
\$part = "1234P";
and display it with number in brackets if exists, so
\$final = "1234P(13)" in this case.
otherwise display "echo \$part.' does not exists';".

Any ideas?
Thanks for any help.
LVL 8
###### Who is Participating?

Commented:
just to build on Amick's, I believe he made a slight mistake on his "\$len2" equation. Try the following:

``````<?php
\$str = "2008T(12)-1234P(13)-111T(12)-908P(45)-11T(11)";
\$part = "1234P";

\$pos1 = strpos(\$str,\$part); // 10
\$len1 = strlen(\$part); //5
\$len2 = strpos(\$str,')',\$pos1+1)+1;  //19
\$final = substr(\$str,\$pos1,\$len2-\$pos1); //1234P(13)

if(\$pos1!==FALSE)
echo "Final: \$final";
else
echo "\$part does not exist";
?>
``````

if you use \$part = "1234P", it will print "Final: 1234P(13)"
if you use \$part = "4321P", it will print "4321P does not exist"
0

Commented:
if \$str = "2008T(12)-1234P(13)-111T(12)-908P(45)-11T(11)";
and \$part = "1234P";
let
\$pos1 = strpos(\$str,\$part); // 10
\$len1 = strlen(\$part); //5
\$len2 = strpos(\$str,')',\$pos1+1)-\$pos1;  //19
\$final = substr(\$str,\$pos1,\$len2-\$len1) //1234P(13)
0

Author Commented:
Thanks, but output of you script is "123" instead "1234P(13)"
0

Author Commented:
Excellent, thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.