473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

realin
254 100+
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_contents
</div>
[/HTML]

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

Thanks :)
Cheers !!
May 14 '08 #1
11 11092
Markus
6,050 Expert 4TB
Would

[php]
preg_match('/<div id=\"".$id."\">(.*?)</div>/', $string, $matches);
[/php]
Work?
May 14 '08 #2
realin
254 100+
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
May 14 '08 #3
realin
254 100+
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]
May 14 '08 #4
Markus
6,050 Expert 4TB
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);
May 14 '08 #5
ronverdonk
4,258 Expert 4TB
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
May 14 '08 #6
realin
254 100+
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 ??
May 15 '08 #7
ronverdonk
4,258 Expert 4TB
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>';
?>
<div id="myId" style="visibility: hidden;">
<?php
echo $str;
?>
</div>
<script type="text/javascript">
if (document.getElementById("static").firstChild.node Name=="#text")
alert ('The div text is: '+document.getElementById("static").firstChild.nod eValue);
</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
May 15 '08 #8
realin
254 100+
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 16 '08 #9
ronverdonk
4,258 Expert 4TB
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 16 '08 #10
realin
254 100+
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 18 '08 #11
ronverdonk
4,258 Expert 4TB
Don't blame yourself for missing that. It works, that's all you need. See you around.

Ronald
May 19 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
1
by: Ian Davies | last post by:
Hello In a php file I have a drop down list with index numbers in whos default value is feed into an sql query that filters records from my database and displays them in an html table. Trouble...
9
by: | last post by:
Is there any sample about getting the results from executing an exe using C#? Thanks for answering.
2
by: Tina | last post by:
I'm trying to get started with Atlas and associated necessary javascript at the same time. I started out at http://atlas.asp.net/walkthroughs/gettingstarted/basic.net where they have an...
1
by: ced69 | last post by:
having trouble getting marquee to work get object required errors tring t <title>This Month at the Chamberlain Civic Center</title> <link href="styles.css" rel="stylesheet"...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
4
by: N00b13 | last post by:
I have a great JS menu but I have to update every page each time I want to change a link. Is there a way to store my links in a file and call it so i only change that file? (what I have tried so far...
9
vikas251074
by: vikas251074 | last post by:
I am not getting date value in spite of my good effort. This code was working in my last office where I work. Now I am trying to work at my home pc. but not getting date value. Any can help me why...
7
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.