Current location: Hot Scripts Forums » Programming Languages » PHP » splitting


splitting

Reply
  #1 (permalink)  
Old 04-18-05, 05:51 AM
Tempestshade Tempestshade is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
splitting

i need to split a URL like
<A href="index.html">Home</a>

I want to get the "Home" Part of the url and exclude the rest anyone know how to do that?
__________________
http://www.cashbackhosting.net - Cashback Hosting. Cheap Web Hosting starting at $1.50
Reply With Quote
  #2 (permalink)  
Old 04-18-05, 08:43 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

The easiest solution is to use strip_tags() function - it will return 'Home' if called with that string.
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #3 (permalink)  
Old 04-18-05, 08:56 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
simple regexp (you can extract it from the command line)
Code:
<M+1|k+3> php -r '$string = "<a href=\"test.html\">TEst</a><a>Test2</A>"; preg_match_all("/<(a)(\s+)?(?(2)[^>]+)>(.*?)<\\/\\1>/si",$string,$matches); print_r($matches);'
Array
(
    [0] => Array
        (
            [0] => <a href="test.html">TEst</a>
            [1] => <a>Test2</A>
        )

    [1] => Array
        (
            [0] => a
            [1] => a
        )

    [2] => Array
        (
            [0] =>
            [1] =>
        )

    [3] => Array
        (
            [0] => TEst
            [1] => Test2
        )

)
regards,
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #4 (permalink)  
Old 04-18-05, 11:54 AM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
YIKES !

You could also use str_replace:

PHP Code:

$string '<A href="index.html">Home</a>';

$string str_replace('<A href="index.html">'""$string);
$string str_replace('</a>'""$string);
print 
$string
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
  #5 (permalink)  
Old 04-18-05, 02:13 PM
Tempestshade Tempestshade is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
hrm... how could i make that possible if the URL just kept changing?
__________________
http://www.cashbackhosting.net - Cashback Hosting. Cheap Web Hosting starting at $1.50
Reply With Quote
  #6 (permalink)  
Old 04-18-05, 06:11 PM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by FiRe
YIKES !

You could also use str_replace:

PHP Code:

$string '<A href="index.html">Home</a>';

$string str_replace('<A href="index.html">'""$string);
$string str_replace('</a>'""$string);
print 
$string
not me, but it's the author who answered why the method is not acceptable
Quote:
Originally Posted by Tempestshade
hrm... how could i make that possible if the URL just kept changing?
more reasons:
1. it won't work for other combinations of link tag (a:A,a:a,A:A)
2. it won't work for whole the file.

if one doesn't care too much about the speed of parsing, use regex (it's just a line or two, compared to several lines for manual userland string-matching procedure). here i write again the code in highlighted manner:
PHP Code:

$string "<a href=\"test.html\">TEst</a><a>Test2</A><a href='go.html'>Go.. go.. go..</A> Won't be included in the string matching: </a>incomplete linking"
preg_match_all("/<(a)(\s+)?(?(2)[^>]+)>(.*?)<\\/\\1>/si",$string,$matches); print_r($matches);
print_r($matches[3]); 
Output:
Code:
Array
(
    [0] => Array
        (
            [0] => <a href="test.html">TEst</a>
            [1] => <a>Test2</A>
            [2] => <a href='go.html'>Go.. go.. go..</A>
        )

    [1] => Array
        (
            [0] => a
            [1] => a
            [2] => a
        )

    [2] => Array
        (
            [0] =>
            [1] =>
            [2] =>
        )

    [3] => Array
        (
            [0] => TEst
            [1] => Test2
            [2] => Go.. go.. go..
        )

)
Array
(
    [0] => TEst
    [1] => Test2
    [2] => Go.. go.. go..
)
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #7 (permalink)  
Old 04-18-05, 07:30 PM
Tempestshade Tempestshade is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Ok lol... i got that to work. But i just want it to display
TEst
not Array blah blah blah.
__________________
http://www.cashbackhosting.net - Cashback Hosting. Cheap Web Hosting starting at $1.50
Reply With Quote
  #8 (permalink)  
Old 04-20-05, 10:13 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
omg.. should i go into details?
PHP Code:

for($i=0$i<count($matches[3]); $i++) {

     print 
$matches[3][$i].'<br />';

and don't forget to uncomment / delete those print_r functions
__________________
just an ignorant noob with moronic solution...
Reply With Quote
Reply

Bookmarks


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting a string perleo PHP 1 01-28-05 09:58 AM
splitting text into pages in php bionicsamir PHP 4 01-05-05 12:58 AM
if(!$a=="") Means? steveo PHP 10 12-15-04 07:19 AM
Splitting Values In A Field mdj1 PHP 7 03-19-04 12:54 AM


All times are GMT -5. The time now is 06:39 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.