Me again...

Like the title says... I'm using this module to fill in a nodes title with the page title of the submitted url.

Now the problem is the I don't want the site name... How would I go about doing this considering every page title is different. some like drupal uses PAGE TITLE | SITE NAME other have it switched the other way around and others... wel do it other ways...

Maybe I'd be better of searching for the first
tag?

Any input would be great. Thank you.

Comments

crystaldawn’s picture

Seems to me that you just need some simple PHP logic. If you have the HTML of the page you want to get the title from and the title is in the format of Something | Here here is how I would approach it. The following is just a quick example, it can be optimized to be a little speedier by not running the before() and after() functions unless they are needed.

//This should return "Using right" since only the right side contains a domain name.  The left side contains spaces and a domain name which is not a
//valid domain name to begin with so it wouldnt match.
$content = "Some HTML Here that contains the page title <title>Its a test.com | title.com</title>";
$title = between('<title>', '</title>', $content, FALSE);  //The false allows $content to contain HTML in it.  It's a security thing for drupal.
$left = trim(before('|', $title));
$right = trim(after('|', $title));

//If the left side of the | is a domain name, use it as the title.
if (is_domain_name($left))
{
   echo "using left";
   $title = $left;
}
else  //Otherwise if left wasnt a domain name, assume the right side was instead and use that.
{
   echo "using right";
   $title = $right;
}

//Set your node title to the title, or use hook_form_alter() to set it in a node edit form or something.
$node->title = $title;

//This is a small utility function that I've yet to port to parsing_api but am considering converting it to use parsing_api to make it easily extensible.  You should be able to use this as it is without much trouble, it's just a monster to read lol.
function is_domain_name($string)
{
   return preg_match("/^([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i", $string);
}
walker2238’s picture

Thanks for the reply... But the problem is when a site uses something like...

The page title | Something else | Site name

And some sites don't use any separators... Or heaven forbid bad mark up such as <title >.

crystaldawn’s picture

Interesting, I like puzzles like this. Try this instead then:

   $content1 = "Some HTML Here that contains the page title test.com <title>Its a | testgwcomeg345345@#$!%#$%T^t|title.com</title> more stuff here";
   $content2 = "Some HTML Here that contains the page title test.com <title>Its a | testgw.comeg345345@#$!%#$%T^t|title.com</title>more stuff here";
   $content3 = "Some HTML Here that contains the page title test.com <title>Its a | testgwcomeg345345@#$!%#$%T^t|title.com more stuff here";
   $content4 = "Some HTML Here that contains the page title test.com <title>Its a | testgw.comeg345345@#$!%#$%T^t|title.com more stuff here";
   $content5 = "Some HTML Here that contains the page title test.com <title>Its a | testgwcomeg345345@#$!%#$%T^t|title.com</title>more stuff here";

   $domain_names[] = parse_domain_name_out_of_title($content1, $include_bad_title_tags = FALSE);
   $domain_names[] = parse_domain_name_out_of_title($content2, $include_bad_title_tags = FALSE);
   $domain_names[] = parse_domain_name_out_of_title($content3, $include_bad_title_tags = TRUE);
   $domain_names[] = parse_domain_name_out_of_title($content4, $include_bad_title_tags = TRUE);
   $domain_names[] = parse_domain_name_out_of_title($content5, $include_bad_title_tags = TRUE);

   print_r($domain_names);
   /*
      The resulting array will look like this:
      Array
(
    [0] => title.com
    [1] => testgw.com
    [2] => title.com
    [3] => testgw.com
    [4] => title.com
)
   */


//This function returns the FIRST domain name that it finds in a <title>.
function parse_domain_name_out_of_title($string, $include_bad_title_tags = FALSE)
   {
      if ($include_bad_title_tags)
      {
         $title = after('<title>', $string);
      }
      else
      {
         $title = between('<title>', '</title>', $string, FALSE);
      }

      //Normal domain names for the US/CA
      $domain_extensions[] = '.us';
      $domain_extensions[] = '.ca';
      $domain_extensions[] = '.com';
      $domain_extensions[] = '.net';
      $domain_extensions[] = '.org';
      $domain_extensions[] = '.gov';
      $domain_extensions[] = '.biz';
      $domain_extensions[] = '.tv';
      $domain_extensions[] = '.info';
      $domain_extensions[] = '.edu';
      $domain_extensions[] = '.mil';

      //Oddball domain names
      $domain_extensions[] = '.aero';
      $domain_extensions[] = '.asia';
      $domain_extensions[] = '.cat';
      $domain_extensions[] = '.coop';
      $domain_extensions[] = '.int';
      $domain_extensions[] = '.mobi';
      $domain_extensions[] = '.museum';
      $domain_extensions[] = '.name';
      $domain_extensions[] = '.pro';
      $domain_extensions[] = '.tel';
      $domain_extensions[] = '.travel';
      $domain_extensions[] = '.xxx';

      //Country level domain names (To many to list here, add as needed.)
      $domain_extensions[] = '.uk';
      $domain_extensions[] = '.jp';
      $domain_extensions[] = '.cn';
      $domain_extensions[] = '.au';

      foreach ($domain_extensions AS $key => $extension)
      {
         $domain_name_prefix = before($extension, $title, FALSE);

         //We have a possible domain name hit
         if ($domain_name_prefix)
         {
            //Check to see if there are any spaces first, if so, then it should be the LAST space where the domain name resides.
            if (strstr($domain_name_prefix, ' '))
            {
               $domain_name = after_last(' ', $domain_name_prefix.$extension);
            }

            //Make sure the first character is alphanumeric.  If its not, strip it off and re-test.  This will knock out any unknown non-alphanumeric
            //separators that may be in use such as | symbols.

            $size = strlen($domain_name);

            while($size--)
            {
               //The only non-alphanumeric chars in a domain name will be . and - so we check for those first.  Then check for alphanumeric.
               if ($domain_name[$size] != '.' && $domain_name[$size] != '-' && !ctype_alnum($domain_name[$size]))
               {
                  $domain_name = after($domain_name[0], $domain_name);  //Knock off the first character in the string.  This works because all strings are also an array.
               }
            }

            if ($domain_name && is_domain_name($domain_name))
            {
               //Houston we have a domain name, stop here and return it :D
               return $domain_name;
            }
         }
      }
   }
   //This is a small utility function that I've yet to port to parsing_api but am considering converting it to use parsing_api to make it easily readable.  You should be able to use this as it is without much trouble, it's just a monster to read lol.
   function is_domain_name($string)
   {
      return preg_match("/^([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i", $string);
   }
walker2238’s picture

Cool... I'll play around with it... DOM seems so much simpler though. Guess I have a lot of testing to do.

Once again thanks a lot for all your help.

crystaldawn’s picture

Status: Active » Closed (works as designed)