-4

i have 1500 products listed on the website. the page titles are all the same by default code below

$title="Detail Turkish Property For Sale in Turkey";

to make the page titles more descriptive about the products, i would like to get the first 10 words from the page product descriptions, to show in the page titles.

i have tried this example with no change,

$title = stripslashes(substr($emlaklist->aciklama,0,80));

your suggestions to solve this issue would be appreciated.

antalya
  • 85
  • 6
  • Do you only want to limit it to the count of words (10) or also to the maximum length of characters? – Iralution Jul 21 '14 at 09:34
  • 1
    It's not enough to tell us what you tried. Also tell us what result did you get, why was it wrong for your needs and what did you expect to get instead. – marekful Jul 21 '14 at 09:35
  • 2
    @antalya - you've asked this exact question elsewhere, what was wrong with the answers on that question? (http://stackoverflow.com/questions/24860018/extract-part-of-description-to-show-in-title) – Hecksa Jul 21 '14 at 09:37
  • i am no php expert and don't want to mess up any page coding that i am not familiar with. i do really appreciate the time and effort put in by the experts here to provide answers for solution, but when there are many answers it gets confusing for me. i have tried all the examples provided but didn't get it to work. Iralution: i would not mind fist 80 chracters or 10 words which ever has a single code solution. i would prefer characters though. – antalya Jul 21 '14 at 10:13
  • what i am after is how to write the $title code correct to get it to work. i am after extracting the description code written in the product description and not the description below the title. there could of been a confusion on my previous question as to which description to extract from. – antalya Jul 21 '14 at 10:14
  • the previous examples just say add this here, try this but is not saying this is the code that will work with full example. i think the code $title= will not work when not within brackets. – antalya Jul 21 '14 at 10:14
  • the current page code is written as below and has brackets $title="Detail Turkish Property For Sale in Turkey"; i have tried to adapt the codes provided with no brackets thus think that could be the reason it didn't work. i have tried them also using brackets i have tried these suggestions and no change after re-checking $title= stripslashes(substr($emlaklist->aciklama,0,80)); $title= "stripslashes(substr($emlaklist->aciklama,0,80));"; $title= substr($string, 80); $title= "substr($string, 80);"; $title= substr($descripton, 0,80); – antalya Jul 21 '14 at 10:14
  • @antalya i think it would be worth while investment to learn basic php programming first before jumping into deeper stuff, a friendly suggestion ... – Ayub Jul 22 '14 at 07:51

4 Answers4

2

Below is a function copied from How can I truncate a string to the first 20 words in PHP?, so I take no credit for this, but it does appear to do what you're looking for.

function limit_text($text, $limit) {
  if (str_word_count($text, 0) > $limit) {
      $words = str_word_count($text, 2);
      $pos = array_keys($words);
      $text = substr($text, 0, $pos[$limit]) . '...';
  }
  return $text;
}
Community
  • 1
  • 1
1

You'll need some way of processing the string as checking for words is slightly more complex than checking for characters. This example is actually taken from the Laravel framework with some minor adaptations to make it work stand alone.

/**
* @param {String} the value to shorten
* @param {Integer} number of words allowed
* @param {String} what to put on the end of the string
* @return {String}
*/
function words($value, $words = 100, $end = '...')
{
    preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);

    if ( ! isset($matches[0])) return $value;

    if (strlen($value) == strlen($matches[0])) return $value;

    return rtrim($matches[0]).$end;
}

Then you can use something like:

$title = words($title, 10, '<a href="#">Read more</a>');

To use this with your code:

$title = words($emlaklist->aciklama, 10, '');

Make sure that the function is declared, just copy paste from here if you want. Also make sure that $emlaklist->aciklama contains what you need.

David Barker
  • 14,484
  • 3
  • 48
  • 77
  • hi i really need help and would appreciate further, but whatever i got advice here i tried, none work. – antalya Jul 28 '14 at 16:52
  • http://www.turkish-property-world.com/detail.php?KKId=2810&Ino=4 is the page where i would like to have the Listing Description (pull the first 8 words) to be up in the website page title. – antalya Jul 28 '14 at 16:54
  • the title code in the top of the page starts like this $title="Detail Turkish Property For Sale in Turkey"; – antalya Jul 28 '14 at 16:56
  • the listing description code starts like this =nl2br($emlak->aciklama);?> – antalya Jul 28 '14 at 16:57
  • ob_start(); session_start(); $title="Property For Sale in Turkey -Property Detail"; – antalya Jul 28 '14 at 17:04
  • Hi Antalya, sorry you couldnt get anything to work. I'm not 100% certain how you're using the functions you've been given above. To help me help you, could you put your actual code in a pastebin (http://phpfiddle.org/) and share it with me. – David Barker Jul 29 '14 at 06:34
  • i have added the page code for detail.php in above original question. – antalya Jul 29 '14 at 07:08
  • Have added an extra bit to the answer... beyond this, I can't say I'll be much more help. – David Barker Jul 29 '14 at 07:21
1

This should work fine for you. Let's say:

$title="Detail Turkish Property For Sale in Turkey";
$description = implode(' ',array_slice(explode(' ', $title), 0, 10));

Here are links to the php manual for explanations of the functions used:

explode function - http://php.net/manual/en/function.explode.php
array_slice function - http://au2.php.net/manual/en/function.array-slice.php
implode function - http://au2.php.net/manual/en/function.implode.php

Goodluck.

1

From what I see in the comments, you will first have to make sure that your variable ($emlaklist) is already set, when you want to append it's value to the $title variable.

In general you might try to copy the code-part that starts with $emlaklist = .. to the place before your $title =, but most likely this will be a database request, so you might need to take other code into account, that contributes to getting that value ...

Other than that, the other answers here will be very well able to handle the shortening of your description, etc.

(Sry that this is too long for a comment, but it might be helpful anyway.)

Levite
  • 17,263
  • 8
  • 50
  • 50