Getting Div's value from a File/String/URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • realin
    Contributor
    • Feb 2007
    • 254

    Getting Div's value from a File/String/URL

    hiya guys,

    Before asking question let me tell you i am really bad at regular expressions (REGEX) :(

    Well to start with, i want to get the value inside a div with a particular id.
    An example can be

    [HTML]<div id="static">
    This is the content i want to fetch, When i have this whole file in the string format which i got it from file_get_conten ts
    </div>
    [/HTML]

    I hope i made my question clear, please let me know how can i do that ?

    Thanks :)
    Cheers !!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Would

    [php]
    preg_match('/<div id=\"".$id."\"> (.*?)</div>/', $string, $matches);
    [/php]
    Work?

    Comment

    • realin
      Contributor
      • Feb 2007
      • 254

      #3
      Originally posted by markusn00b
      Would

      [php]
      preg_match('/<div id=\"".$id."\"> (.*?)</div>/', $string, $matches);
      [/php]
      Work?
      really thanks a lot for quick response, but can i do it without regex ??
      i mean isnt there any way out ??

      though i reckon,this is the best/only way :)

      cheers !! thanks again

      Comment

      • realin
        Contributor
        • Feb 2007
        • 254

        #4
        well writing this would give me error,

        [PHP]preg_match('/<div id=\"normal\">( .*?)</div>/', $getWholePage, $matches);

        Warning: preg_match() [function.preg-match]: Unknown modifier 'd' in D:\xamp\htdocs\ cut\index.php on line 14
        [/PHP]

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by realin
          well writing this would give me error,

          [PHP]preg_match('/<div id=\"normal\">( .*?)</div>/', $getWholePage, $matches);

          Warning: preg_match() [function.preg-match]: Unknown modifier 'd' in D:\xamp\htdocs\ cut\index.php on line 14
          [/PHP]
          preg_match('/<div id=\"normal\">( .*?)</div>/', $getWholePage, $matches);

          The delimiter is a forward slash, so you need to escape it in the closing div tag.

          preg_match('/<div id=\"normal\">( .*?)<\/div>/', $getWholePage, $matches);

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Although this is the PHP forum, so my post does not really belong here, I'd like you to consider a JavaScript solution (since you do not want to use a regexp). See this sample[code=html]<div id="static">
            This is the content i want to fetch, When i have this whole file in the string format which i got it from file_get_conten ts
            </div>
            <script type="text/javascript">
            if (document.getEl ementById("stat ic").firstChild .nodeName=="#te xt")
            alert ('The div text is: '+document.getE lementById("sta tic").firstChil d.nodeValue);
            </script>[/code]Ronald

            Comment

            • realin
              Contributor
              • Feb 2007
              • 254

              #7
              Originally posted by ronverdonk
              Although this is the PHP forum, so my post does not really belong here, I'd like you to consider a JavaScript solution (since you do not want to use a regexp). See this sample[code=html]<div id="static">
              This is the content i want to fetch, When i have this whole file in the string format which i got it from file_get_conten ts
              </div>
              <script type="text/javascript">
              if (document.getEl ementById("stat ic").firstChild .nodeName=="#te xt")
              alert ('The div text is: '+document.getE lementById("sta tic").firstChil d.nodeValue);
              </script>[/code]Ronald
              but will this script work for a string ??
              As i mentioned i am getting a file contents using PHP function..
              I guess JS works on browser and when the current page is loaded..
              Say if i am on page X i want to get the contents of a div which is stored in varibale $str.
              Is that possible using JS ??

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                I really don't know what you want to achieve or why. But assuming the complete div is in a PHP variable $str and you wanted to get only the 'between' div's text from that php variable using JS. In order to prevent passing the PHP variable to JS, I would show the div (from the variable) within another div with visibility=hidd en and run the JS. Like this[php]<?php
                $str='<div id="static">Thi s is the content i want to fetch, When i have this whole file in the string format which i got it from file_get_conten ts</div>';
                ?>
                <div id="myId" style="visibili ty: hidden;">
                <?php
                echo $str;
                ?>
                </div>
                <script type="text/javascript">
                if (document.getEl ementById("stat ic").firstChild .nodeName=="#te xt")
                alert ('The div text is: '+document.getE lementById("sta tic").firstChil d.nodeValue);
                </script>[/php]But when the above is not what you are looking for then I don't understand your problem and, in that case, please elaborate a bit more.

                Ronald

                Comment

                • realin
                  Contributor
                  • Feb 2007
                  • 254

                  #9
                  Originally posted by ronverdonk
                  I really don't know what you want to achieve or why. But assuming the complete div is in a PHP variable $str and you wanted to get only the 'between' div's text from that php variable using JS. In order to prevent passing the PHP variable to JS, I would show the div (from the variable) within another div with visibility=hidd en and run the JS. Like this[php]<?php
                  $str='<div id="static">Thi s is the content i want to fetch, When i have this whole file in the string format which i got it from file_get_conten ts</div>';
                  ?>
                  But when the above is not what you are looking for then I don't understand your problem and, in that case, please elaborate a bit more.

                  Ronald
                  Wow,

                  thanks, i guess this should work. I will check when i get back home..
                  you are always a life saver bro ;)
                  thx
                  cheers !!
                  Realin !

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    So you mean I understood your question.proble m? If that is the case I hope that solution works for you. I'm always in for a coding challenge.

                    Ronald

                    Comment

                    • realin
                      Contributor
                      • Feb 2007
                      • 254

                      #11
                      yup ronanld,

                      the solution worked for me, i used more smarter jquery :) but the idea was yours to display the string, how can i miss that :p

                      hehehe.. anyways thanks a lot :)

                      Comment

                      • ronverdonk
                        Recognized Expert Specialist
                        • Jul 2006
                        • 4259

                        #12
                        Don't blame yourself for missing that. It works, that's all you need. See you around.

                        Ronald

                        Comment

                        Working...