Control on title

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Geoserv
Forum Newbie
Posts: 3
Joined: Fri Jan 25, 2008 9:20 am

Control on title

Post by Geoserv »

I use this to fetch the title of a webpage submitted to my website:

Code: Select all

if(preg_match("'<title>([^<]*?)</title>'", $this->html, $matches)) {
            $this->url_title=trim($matches[1]);
What I'm wondering is if I can control titles formatted like this:
KnotGypsy's Kitchen Chronicles: Cinnamon Lemon Cookies
and remove:
KnotGypsy's Kitchen Chronicles:
is this possible with PHP?

Geoserv
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Control on title

Post by aceconcepts »

You could use explode().

See http://uk2.php.net/explode
Geoserv
Forum Newbie
Posts: 3
Joined: Fri Jan 25, 2008 9:20 am

Re: Control on title

Post by Geoserv »

Thanks for the quick reply, but after reviewing that page, I am totally lost as to how to implement that into my current code.

Can you offer some assistance using the code provided in post 1?

Geoserv
Geoserv
Forum Newbie
Posts: 3
Joined: Fri Jan 25, 2008 9:20 am

Re: Control on title

Post by Geoserv »

I have come up with this:
if(preg_match("'<title>([^<]*?)</title>'", $this->html, $matches)) {
$this->url_title=trim($matches[1]);
list($notused, $strTitle) = explode(": ", $this->url_title);
but I get a parse error:
Parse error: syntax error, unexpected T_CLASS, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/newsdots/public_html/libs/link.php on line 945
Not sure what about adding that line is causing the problem.

Geoserv.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Control on title

Post by JAM »

I'd use str_replace myself as we are dealing with a string... Perhaps something like;

Code: Select all

   // untested
    if (preg_match("'<title>([^<]*?)</title>'", $this->html, $matches)) {
        $this->url_title = str_replace('KnotGypsy\'s Kitchen Chronicles: ', '', trim($matches[1]));
    } else {
        $this->url_title = 'No title';
    }
Edit: There is a bug in the

Code: Select all

tag.[/b] The 'KnotGypsy's Kitchen Chronicles: ' should be 'KnotGypsy[b]\'[/b]s Kitchen Chronicles: ' in the above...
Or we could all use double quotes, but I personally don't change my typing because the forumsoftware wants me to.
Post Reply