1

For example:

 $string="<aa>xyz<bb>123<ccc>";

I want to get the substring 'xyz' from $string.Is it possible?

2
  • 3
    The pony he comes... Commented Sep 17, 2012 at 7:13
  • ok, since you have the substrings that are to the left and right of the substring you want, do ltrim and rtrim of left and right substrings on main string to get what you want. <insert evil smile> Commented Sep 17, 2012 at 7:15

4 Answers 4

3

Simply you can use strip_tags to get xyz

<?php
      echo strip_tags('<aa>xyz<bb>');
    ?>

strip_tags is only striping tags. Use it only if this meets your requirement.

Sign up to request clarification or add additional context in comments.

Comments

2

You can also use a regex to explode the string into an array. this would give you the substring you want in $arr[0] This will work regardless what the tags are, and allow you to easily which ever substring you want.

<?php
$string="<aa>xyz<bb>123<ccc>";
$arr = preg_split("/<[^>]+>/", $string, 0, PREG_SPLIT_NO_EMPTY);
var_export($arr);

Comments

2

yes it is possible

 $string="<aa>xyz<bb>";

 echo substr($string,4,4);

you can also do this by strip_tags() it Strip HTML and PHP tags from a string

Comments

1

Use preg_match with <aa>(.*?)</bb> , it will be the first match.

With regular expressions you will also be able to get other matches in your string also ..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.