0

Hi i was looking to find an exact php equivalent of this java code

 List<String> strings = Arrays.asList(tempFinalString.replaceAll("^.*?>&", "")
            .split("<.*?(>&|$)"));

This gives an array which contain all the characters between the two characters ?> and <

Can anybody help me to find its exact php equivalent?

I searched a lot please help

3 Answers 3

1

Refer this link

http://forums.devshed.com/php-development-5/values-string-tags-578670.html

And change your code like this,

preg_match_all("#<abc([^<]+)>#", $yourstring, $ansvariable); 
//echo implode("::", $ansvariable[1]); 


foreach($yourstring[1] as $key => $val){
echo $yourstring[1][$key]."<br>"; // prints $val
}
Sign up to request clarification or add additional context in comments.

Comments

1

You may use pregsplit, it is pretty much the same and returns a list of strings.

Comments

1

You can use regex for this

<?php
function getInbetweenStrings($start, $end, $str){
    $matches = array();
    $regex = "/$start([a-zA-Z0-9_]*)$end/";
    preg_match_all($regex, $str, $matches);
    return $matches[1];
}

$str = "<asdfasf?>dsdafs<asfasdfasf?>";
$str_arr = getInbetweenStrings('<', '\?>', $str);

print_r($str_arr);

The quite similar question is this one: Get substring between two strings PHP

EDIT

Of course for different arguments you need to change the function arguments :-)

$str = "<abcasdfasf>dsdafs<abcsfasdfasf>";
$str_arr = getInbetweenStrings('<abc', '>', $str);

print_r($str_arr);

Also be aware that the code will split the string that contains only specific characters - [a-zA-Z0-9_].

Consider learning regular expression first a bit.

Cheers!

1 Comment

i checked your code, its not working, its working fine for < and ?> but not working for <abc and >, please help i want this type of spliting and filtering not for single characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.