Jump to content
xisto Community
Sign in to follow this  
ChronicLoser

First Time Programming In Php quick question

Recommended Posts

This is my first time actually programming with PHP. I used to only use it's dynamic inclusion abilities so i never really learnt about the functions of objects and such.

Alright, on to the question. How would i fetch the text inside a span tag using php? Sorry, I'm not too sure how to explain this... so let me give an example instead:

<span id="span1">comment1</span><span id="span2">comment2</span>
Is it possible to get only the 'comment1' and 'comment2' out of the span1 and span2 using php then echo it somewhere else on the same page? I know javascript could do it but I'm not too sure about the capablities of php. And it doesn't have to be the span tag. If it also works with <div> or <table> i'll be happy too =P

thanks in advance ^_^

Share this post


Link to post
Share on other sites

but what if i do not know how the comment looks like? The way you're showing me is if i knew exactly what the entire code looks like. Say i do not know what's within span1 and span2 but i still want to draw it out using php and echo the comment somewhere else on the page.thanks for the attempt anyways ^_^

Share this post


Link to post
Share on other sites

heh...sorry for double posting...but i really need an answer to my question soon. I kinda have a deadline i want to meet. So here's my question again: how can i use PHP to draw out a comment/word from inside a <span> tag with a specific id/name/class and echo it or set it to a variable so that i could use it somewhere else on the page? I do not know what is in the <span> because it will constantly be changing depending on the user viewing the page with the code. Hope I was clear enough this time...

Share this post


Link to post
Share on other sites

HiYou can try this:Bla bla bla<br><span id="r">fha</span><br>bla<br><span> fsd </span><br><span id="er"><b>fhs</b></span><br>bla<br><br><?php//$a = $_SERVER['PHP_SELF'];$a = 'a.php';$wholefile = file_get_contents($a);preg_match_all("|<span id=\"er\">.*</span>|U",$wholefile, $out, PREG_PATTERN_ORDER);echo $out[0][0];?>The things above <?php> are the source file (in my case a.php, but you can also use the current file using the PHP_SELF constant).The match function searches for matches to the <span id="er">*****</span> filter.The result is an array of results, the first (and only in this case) is $out[0][0].CheersRuben

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.