Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

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

Question posted by: realin (Familiar Sight) on May 14th, 2008 06:35 PM
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

Expand|Select|Wrap|Line Numbers
  1. <div id="static">
  2. 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_contents
  3. </div>


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

Thanks :)
Cheers !!
Markus's Avatar
Markus
Moderator
2,217 Posts
May 14th, 2008
06:41 PM
#2

Re: Getting Div's value from a File/String/URL
Would

Expand|Select|Wrap|Line Numbers
  1. preg_match('/<div id=\"".$id."\">(.*?)</div>/', $string, $matches);

Work?

Reply
realin's Avatar
realin
Familiar Sight
249 Posts
May 14th, 2008
06:43 PM
#3

Re: Getting Div's value from a File/String/URL
Quote:
Would

Expand|Select|Wrap|Line Numbers
  1. preg_match('/<div id=\"".$id."\">(.*?)</div>/', $string, $matches);

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

Reply
realin's Avatar
realin
Familiar Sight
249 Posts
May 14th, 2008
06:48 PM
#4

Re: Getting Div's value from a File/String/URL
well writing this would give me error,

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

Reply
Markus's Avatar
Markus
Moderator
2,217 Posts
May 14th, 2008
08:30 PM
#5

Re: Getting Div's value from a File/String/URL
Quote:
well writing this would give me error,

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


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);

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
May 14th, 2008
10:31 PM
#6

Re: Getting Div's value from a File/String/URL
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
Expand|Select|Wrap|Line Numbers
  1. <div id="static">
  2. 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_contents
  3. </div>
  4. <script type="text/javascript">
  5. if (document.getElementById("static").firstChild.nodeName=="#text")
  6. alert ('The div text is: '+document.getElementById("static").firstChild.nodeValue);
  7. </script>
Ronald

Reply
realin's Avatar
realin
Familiar Sight
249 Posts
May 15th, 2008
03:07 AM
#7

Re: Getting Div's value from a File/String/URL
Quote:
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
Expand|Select|Wrap|Line Numbers
  1. <div id="static">
  2. 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_contents
  3. </div>
  4. <script type="text/javascript">
  5. if (document.getElementById("static").firstChild.nodeName=="#text")
  6. alert ('The div text is: '+document.getElementById("static").firstChild.nodeValue);
  7. </script>
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 ??

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
May 15th, 2008
10:08 PM
#8

Re: Getting Div's value from a File/String/URL
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=hidden and run the JS. Like this
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $str='<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_contents</div>';
  3. ?>
  4. <div id="myId" style="visibility: hidden;">
  5. <?php
  6. echo $str;
  7. ?>
  8. </div>
  9. <script type="text/javascript">
  10. if (document.getElementById("static").firstChild.nodeName=="#text")
  11. alert ('The div text is: '+document.getElementById("static").firstChild.nodeValue);
  12. </script>
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

Reply
realin's Avatar
realin
Familiar Sight
249 Posts
May 16th, 2008
10:43 AM
#9

Re: Getting Div's value from a File/String/URL
Quote:
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=hidden and run the JS. Like this[php]<?php
$str='<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_contents</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 !

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
May 16th, 2008
11:14 PM
#10

Re: Getting Div's value from a File/String/URL
So you mean I understood your question.problem? If that is the case I hope that solution works for you. I'm always in for a coding challenge.

Ronald

Reply
realin's Avatar
realin
Familiar Sight
249 Posts
May 18th, 2008
12:43 PM
#11

Re: Getting Div's value from a File/String/URL
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 :)

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
May 19th, 2008
04:30 PM
#12

Re: Getting Div's value from a File/String/URL
Don't blame yourself for missing that. It works, that's all you need. See you around.

Ronald

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,871 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top PHP Forum Contributors