Page 1 of 1
Control on title
Posted: Fri Jan 25, 2008 9:23 am
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
Re: Control on title
Posted: Fri Jan 25, 2008 9:29 am
by aceconcepts
You could use explode().
See
http://uk2.php.net/explode
Re: Control on title
Posted: Fri Jan 25, 2008 9:44 am
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
Re: Control on title
Posted: Fri Jan 25, 2008 1:42 pm
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.
Re: Control on title
Posted: Sat Jan 26, 2008 5:09 am
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.