Results 1 to 3 of 3
  1. #1
    thorbcn is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    the String class is your friend here.
    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;
    }
    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.
    Last edited by travishein; 01-02-2011 at 03:03 AM.

  3. #3
    thorbcn is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  1. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  2. help with function in java!!!!
    By ricky in forum New To Java
    Replies: 6
    Last Post: 10-12-2009, 01:24 PM
  3. How to use a function in java
    By olikhvar in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 02:57 AM
  4. Use Perl function with java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 AM
  5. Help with recursive function in java
    By cachi in forum Advanced Java
    Replies: 2
    Last Post: 07-31-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •