1

For example:

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

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

Soojoo
  • 2,146
  • 1
  • 30
  • 56
  • 3
    [The pony he comes...](http://stackoverflow.com/a/1732454/554546) –  Sep 17 '12 at 07: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. – Prasanth Sep 17 '12 at 07:15

4 Answers4

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.

Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
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

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
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);
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 ..

Dumitrescu Bogdan
  • 7,127
  • 2
  • 23
  • 31