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
- <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>
I hope i made my question clear, please let me know how can i do that ?
Thanks :)
Cheers !!
|
|
May 14th, 2008 06:41 PM
# 2
|
Re: Getting Div's value from a File/String/URL
Would
-
preg_match('/<div id=\"".$id."\">(.*?)</div>/', $string, $matches);
Work?
|
|
May 14th, 2008 06:43 PM
# 3
|
Re: Getting Div's value from a File/String/URL
Quote:
Would
-
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
|
|
May 14th, 2008 06:48 PM
# 4
|
Re: Getting Div's value from a File/String/URL
well writing this would give me error,
- 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
|
|
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,
- 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
|
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);
|
|
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
- <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>
-
<script type="text/javascript">
-
if (document.getElementById("static").firstChild.nodeName=="#text")
-
alert ('The div text is: '+document.getElementById("static").firstChild.nodeValue);
-
</script>
Ronald
|
|
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
- <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>
-
<script type="text/javascript">
-
if (document.getElementById("static").firstChild.nodeName=="#text")
-
alert ('The div text is: '+document.getElementById("static").firstChild.nodeValue);
-
</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 ??
|
|
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
- <?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>';
-
?>
-
<div id="myId" style="visibility: hidden;">
-
<?php
-
echo $str;
-
?>
-
</div>
-
<script type="text/javascript">
-
if (document.getElementById("static").firstChild.nodeName=="#text")
-
alert ('The div text is: '+document.getElementById("static").firstChild.nodeValue);
-
</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
|
|
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 !
|
|
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
|
|
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 :)
|
|
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
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
|