Results 1 to 3 of 3
Thread: php function to java
- 01-02-2011, 02:32 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
php function to java
hi,
have this php function:
function getDataFromReferer($html,$referer,$ini,$end)
{
$index = stripos($html,$referer,0)+strlen($referer);
if ($index > 0)
{
$index_ini = stripos($html,$ini,$index)+strlen($ini);
$index_fin = stripos($html,$end,$index_ini);
return substr($html,$index_ini,$index_fin-$index_ini);
}
else
{
echo '';
}
}
to use for example:
$parse = getDataFromReferer($page,'</head>','<body>','</body>');
i get all the content starting at </head> from <body> to </body>
from $page where $page is a php string(url content with curl),
works really well for me,
could someone code a java version of the same function or
tell me where you get something close, get part of a string
like that,
i don´t have experience in java to do it
thanks a lot
- 01-02-2011, 03:01 AM #2
the String class is your friend here.
guess this could do more basic checking, in the event that the input html string did not contain <body> or </body> for example., but this is more how the original PHP function worked as it is now.Java Code:public String getDataFromReferer(String in, String referer, String ini, String end) { String result = ""; if (in.indexOf(referer) > -1) { int index = in.indexOf(referer) + referer.length(); int indexIni = in.indexOf(ini, index) + ini.length(); int indexFin = in.indexOf(end, indexIni); result = in.substring(indexIni, indexFin); } return result; }Last edited by travishein; 01-02-2011 at 03:03 AM.
- 07-04-2011, 12:40 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
this class is perfect to get data between 2 points, one time or some many as we find,
import java.util.ArrayList;
import java.util.List;
/**
*
* @author thor
*/
public class stringUtils {
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
public static String substringBetween(String str, String open, String close) {
if (str == null || open == null || close == null) {
return null;
}
int start = str.indexOf(open);
if (start != -1) {
int end = str.indexOf(close, start + open.length());
if (end != -1) {
return str.substring(start + open.length(), end);
}
}
return null;
}
public static String[] substringsBetween(String str, String open, String close) {
if (str == null || isEmpty(open) || isEmpty(close)) {
return null;
}
int strLen = str.length();
if (strLen == 0) {
return null;
}
int closeLen = close.length();
int openLen = open.length();
List<String> list = new ArrayList<String>();
int pos = 0;
while (pos < (strLen - closeLen)) {
int start = str.indexOf(open, pos);
if (start < 0) {
break;
}
start += openLen;
int end = str.indexOf(close, start);
if (end < 0) {
break;
}
list.add(str.substring(start, end));
pos = end + closeLen;
}
if (list.isEmpty()) {
return null;
}
return (String[]) list.toArray(new String [list.size()]);
}
}
Similar Threads
-
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
help with function in java!!!!
By ricky in forum New To JavaReplies: 6Last Post: 10-12-2009, 01:24 PM -
How to use a function in java
By olikhvar in forum New To JavaReplies: 2Last Post: 02-16-2008, 02:57 AM -
Use Perl function with java
By lenny in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 06:25 AM -
Help with recursive function in java
By cachi in forum Advanced JavaReplies: 2Last Post: 07-31-2007, 06:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks