NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page String Manipulation Help

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 10-23-2009, 12:33 PM THREAD STARTER               #1 (permalink)
Ik
Quality //
Join Date: Sep 2008
Posts: 911
Ik has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud of
 


Breast Cancer Save a Life Alzheimer's Wildlife Wildlife

Help! String Manipulation Help


Please see the code below.
????: NamePros.com http://www.namepros.com/programming/618564-string-manipulation-help.html

Code:
<ul>
	<li>
		Services
		<ul>
			<li>
				Web Design & Development
				<ul>
					<li>CMS</li>
					<li>e-Commerce</li>
				</ul>
			</li>
			<li>SEO</li>
			<li>Hosting</li>
		</ul>
		<li>About</li>
		<li>Contact</li>
	</li>
</ul>
I have it in a string variable, and I want to be able to get the 3rd level UL from it. So here is what I want to get:

Code:
<ul>
<li>CMS</li>
<li>e-Commerce</li>
</ul>
Your help is appreciated. I'm using PHP 5
Ik is offline   Reply With Quote
Old 10-23-2009, 01:10 PM   #2 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,963
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
I would have a look at the PHP DOM parser to traverse the HTML structure, other XML libraries should do the job as well.
__________________
NameNewsletter.com - free lists of available domain names
ZoneFiles.net (beta) - ccTLD and gTLD droplists
sdsinc is offline   Reply With Quote
Old 10-24-2009, 01:08 PM   #3 (permalink)
NamePros Regular
 
qbert220's Avatar
Join Date: Jul 2007
Location: UK
Posts: 394
qbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to behold
 


Protect Our Planet
Or you could use preg_match like this (untested):

Code:
if (preg_match('#^.*?<ul>.*?<ul>.*?(<ul>.*?</ul>)#i', $matches))
{
    echo $matches[1];
}
qbert220 is offline   Reply With Quote
Old 10-25-2009, 05:05 PM THREAD STARTER               #4 (permalink)
Ik
Quality //
Join Date: Sep 2008
Posts: 911
Ik has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud of
 


Breast Cancer Save a Life Alzheimer's Wildlife Wildlife
Originally Posted by qbert220 View Post
Or you could use preg_match like this (untested):

Code:
if (preg_match('#^.*?<ul>.*?<ul>.*?(<ul>.*?</ul>)#i', $matches))
{
    echo $matches[1];
}
It seems to me that preg_match is the solution, but the code above is not working anf I'm not too good with regular expressions.
Ik is offline   Reply With Quote
Old 10-25-2009, 08:57 PM   #5 (permalink)
Senior Member
Join Date: Apr 2005
Location: Joliet, Illinois
Posts: 1,177
RageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to behold
 


Child Abuse
Well, what exactly are you looking for?

The best thing to do is to find a single regex that searches for all that you're looking for. Else, you can use a for(); loop or similar to go through multiple regex queries, but preg_match(); will show greater signs of slow downs than str_match();

-RageD
RageD is offline   Reply With Quote
Old 10-26-2009, 05:15 AM   #6 (permalink)
NamePros Regular
 
qbert220's Avatar
Join Date: Jul 2007
Location: UK
Posts: 394
qbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to behold
 


Protect Our Planet
Tested working code:

PHP Code:
<?php
????: NamePros.com http://www.namepros.com/showthread.php?t=618564

$string 
'<ul>
        <li>
                Services
                <ul>
                        <li>
                                Web Design & Development
                                <ul>
                                        <li>CMS</li>
????: NamePros.com http://www.namepros.com/showthread.php?t=618564
                                        <li>e-Commerce</li>
                                </ul>
                        </li>
                        <li>SEO</li>
                        <li>Hosting</li>
                </ul>
                <li>About</li>
                <li>Contact</li>
        </li>
</ul>
'
;

if (
preg_match('#^.*?<ul>.*?<ul>.*?(\s*<ul>.*?</ul>)#is'$string$matches))
{
    echo 
$matches[1]."\n";
}

?>
This should display:

Code:
                                <ul>
                                        <li>CMS</li>
                                        <li>e-Commerce</li>
                                </ul>
In the preg_match:

"^" means match the start of the string
"." means match any character
"*" means zero or more of the previous element
"?" means non-greedy (matches as little as possible - be default the regexp will match as many characters as possible)
"\s" means match whitespace characters

The "i" modifier means cases insensitive
The "s" modifier enables multi-line matching

So ".*" means match zero or more of any character. Adding the "?" means match as few characters as possible. I added the "\s*" to also capture the indenting before the last <td>.
qbert220 is offline   Reply With Quote
Old 10-26-2009, 05:34 AM THREAD STARTER               #7 (permalink)
Ik
Quality //
Join Date: Sep 2008
Posts: 911
Ik has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud of
 


Breast Cancer Save a Life Alzheimer's Wildlife Wildlife
That is perfect. Thanks a lot for the explanation as well

Originally Posted by qbert220 View Post
Tested working code:

...snip...
Last edited by qbert220; 10-26-2009 at 09:05 AM. Reason: Snipping quote
Ik is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:31 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger