For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > November 2007 > take text from a string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author take text from a string
artev

2007-11-13, 7:00 pm

I would like to extract a piece of string inner oneother;
how rule for extraction all text inner this ">.......</a>


example if I have this string
<li><a href="http://localhost/namesitos/index.php?pg=id=b8f">Title &
Test</a></li>

the code will return Title & Test
Thomas Mlynarczyk

2007-11-13, 7:00 pm

Also sprach artev:

> I would like to extract a piece of string inner oneother;
> how rule for extraction all text inner this ">.......</a>
> example if I have this string
> <li><a href="http://localhost/namesitos/index.php?pg=id=b8f">Title
> & Test</a></li>
> the code will return Title & Test



To get just the text inside an <a> element, you can try something like this
(untested):

preg_match( '°\<a\s+[^>]+\>(.*?)\</a\>°', $text, $matches )

$matches[1] will then contain the text.

But if you want to remove all HTML, have a look at PHP's strip_tags()
function.

Greetings,
Thomas

--
C'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)


artev

2007-11-13, 7:00 pm

Il Tue, 13 Nov 2007 16:49:00 +0100, Thomas Mlynarczyk ha scritto:

> Also sprach artev:
>
> To get just the text inside an <a> element, you can try something like this
> (untested):
>
> preg_match( '°\<a\s+[^>]+\>(.*?)\</a\>°', $text, $matches )


> $matches[1] will then contain the text.



work, thanks;
so this is the code for to extract from a link the text?
is a standard code or you have make for yourself ?




> But if you want to remove all HTML, have a look at PHP's strip_tags()
> function.
>
> Greetings,
> Thomas

Thomas Mlynarczyk

2007-11-13, 7:00 pm

Also sprach artev:

[color=darkred]
> work, thanks;
> so this is the code for to extract from a link the text?


This is one way to do it. Note, that, if the link contains other HTML
elements (like a <span>, for example), those will be returned as part of the
text. Also, the above code only returns the text from the first link it
finds. If you have several links and need the text from all of them, have a
look at preg_match_all().

> is a standard code or you have make for yourself ?



It is a regular expression that I made up to show you a way to a possible
solution. Depending on your exact requirements, you might need to do some
modifications on that code.

Greetings,
Thomas

--
C'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)


Onideus Mad Hatter

2007-11-14, 7:00 pm

On Tue, 13 Nov 2007 16:34:53 +0100, artev <mailnotspammm@notspamm.nn>
wrote:

>I would like to extract a piece of string inner oneother;
>how rule for extraction all text inner this ">.......</a>
>
>
>example if I have this string
><li><a href="http://localhost/namesitos/index.php?pg=id=b8f">Title &
>Test</a></li>
>
>the code will return Title & Test


....your question makes no sense because you aren't providing any sort
of coherent basis for it. There are lots of different ways to
manipulate strings in PHP, just look through here:
http://us3.php.net/manual/en/ref.strings.php

....if you can't figure your way out from there you probably shouldn't
be trying to program...in anything.

--

Onideus Mad Hatter
mhm ¹ x ¹
http://www.backwater-productions.net
http://www.backwater-productions.net/hatter-blog


Hatter Quotes
-------------
"You're only one of the best if you're striving to become one of the
best."

"I didn't make reality, Sunshine, I just verbally XXXXX slapped you
with it."

"I'm not a professional, I'm an artist."

"Your Usenet blinders are my best friend."

"Usenet Filters - Learn to shut yourself the XXXX up!"

"Drugs killed Jesus you know...oh wait, no, that was the Jews, my
bad."

"There are clingy things in the grass...burrs 'n such...mmmm..."

"The more I learn the more I'm killing my idols."

"Is it wrong to incur and then use the hate ridden, vengeful stupidity
of complete strangers in random Usenet froups to further my art?"

"Freedom is only a concept, like race it's merely a social construct
that doesn't really exist outside of your ability to convince others
of its relevancy."

"Next time slow up a lil, then maybe you won't jump the gun and start
creamin yer panties before it's time to pop the champagne proper."

"Reality is directly proportionate to how creative you are."

"People are pretty XXXXing high on themselves if they think that
they're just born with a soul. *snicker*...yeah, like they're just
givin em out for free."

"Quible, quible said the Hare. Quite a lot of quibling...everywhere.
So the Hare took a long stare and decided at best, to leave the rest,
to their merry little mess."

"There's a difference between 'bad' and 'so earth shatteringly
horrible it makes the angels scream in terror as they violently rip
their heads off, their blood spraying into the faces of a thousand
sweet innocent horrified children, who will forever have the terrible
images burned into their tiny little minds'."

"How that you're such a poor judge of style that you can't even
properly gauge the artistic worth of your own efforts."

"Those who record history are those who control history."

"I am the living embodiment of hell itself in all its tormentive rage,
endless suffering, unfathomable pain and unending horror...but you
don't get sent to me...I come for you."

"Ideally in a fight I'd want a BGM-109A with a W80 250 kiloton
tactical thermonuclear fusion based war head."

"Tell me, would you describe yourself more as a process or a
function?"

"Apparently this group has got the market cornered on stupid.
Intelligence is down 137 points across the board and the forecast
indicates an increase in Webtv users."

"Is my .sig delimiter broken? Really? You're sure? Awww,
gee...that's too bad...for YOU!" `, )
Sir Robin

2007-11-18, 6:59 pm

On Wed, 14 Nov 2007 06:54:59 -0800, Onideus Mad Hatter
<usenet@backwater-productions.net> wrote:

>...if you can't figure your way out from there you probably shouldn't
>be trying to program...in anything.


Bullshit! When I started programming I couldnt even understand why I a
line with string that had only numbers could not be used in
arithmethic declarations.... After that I had many problems where
someone like you could have said something like that. God bless I'm
smart enough to ignore such comments, as if I wouldnt be I would never
developed to be fairly good (though not guru) programmer.

--
***/--- Sir Robin (aka Jani Saksa) Bi-Sex and proud of it! ---\***
**/ email: robsku@fiveam.NO-SPAM.org, <*> Reg. Linux user #290577 \**
*| Me, Drugs, DooM, Photos, Writings... http://soul.fiveam.org/robsku|*
**\--- GSM/SMS: +358 44 927 3992 ---/**
Kun nuorille opetetaan, että kannabis on yhtä vaarallista kuin heroiini,
niin tokihan he oppivat, että heroiini on yhtä vaaratonta kuin kannabis.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com