• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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.
0
Zado
Asked:
Zado
  • 2
2 Solutions
 
AmickCommented:
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
 
ZadoAuthor Commented:
Thanks, but output of you script is "123" instead "1234P(13)"
0
 
m4trixCommented:
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";
?>

Open in new window


if you use $part = "1234P", it will print "Final: 1234P(13)"
if you use $part = "4321P", it will print "4321P does not exist"
0
 
ZadoAuthor Commented:
Excellent, thanks.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now