2009-08-05 10 views
47

मुझे पता है कि सबस्ट्र फ़ंक्शन का उपयोग कैसे करें, लेकिन यह एक शब्द के माध्यम से आधा रास्ते को समाप्त करने में प्रसन्न होगा। मैं चाहता हूं कि एक शब्द के अंत में स्ट्रिंग समाप्त हो जाए, मैं इसे करने के बारे में कैसे जाऊं? क्या इसमें नियमित अभिव्यक्ति शामिल होगी? किसी भी मदद की बहुत सराहना की।यह सुनिश्चित करना कि किसी शब्द पर PHP सबस्ट्रेट समाप्त नहीं होता है

यह मेरे पास अब तक है। बस substr ...

echo substr("$body",0,260); 

चीयर्स

+0

इस लिंक का प्रयास करें, आपको मदद मिल सकती ... http://stackoverflow.com/a/26098951/3944217 – edCoder

+4

एक असंबंधित मामले में, '$ body 'के बजाय' $ "शरीर का उपयोग क्यों करें? – TRiG

उत्तर

91

यह एक regex के साथ किया जा सकता है, कुछ इस तरह 260 अक्षरों तक स्ट्रिंग के शुरू से ही एक शब्द सीमा से ऊपर हो जाएगा:

$line=$body; 
if (preg_match('/^.{1,260}\b/s', $body, $match)) 
{ 
    $line=$match[0]; 
} 

वैकल्पिक रूप से, आप शायद, wordwrap समारोह इस्तेमाल कर सकते हैं लाइनों में अपने $ शरीर को तोड़ने के लिए तो बस पहली पंक्ति निकालें।

+22

एक डाउनवोट! वहां कोई व्यक्ति नियमित अभिव्यक्ति से नफरत करता है! –

+0

मुझे लगता है कि वे मतदान कर सकते हैं क्योंकि यह PHP का उपयोग नहीं कर रहा है जो जानता है। बहुत धन्यवाद करता है। –

+0

सबसे खराब-डाउनवोट कभी - बी/सी यह अभी भी काम करता है ... 2.5 साल बाद – jbnunn

21

आप इस कोशिश कर सकते हैं:

$s = substr($string, 0, 261); 
    $result = substr($s, 0, strrpos($s, ' ')); 
+1

यह सबसे अच्छा जवाब है! @achshar समाधान के विपरीत, यह समाधान शब्द सीमा से अधिक नहीं होने पर पूरे शब्दों को संरक्षित करने की अनुमति देता है। कुंजी पहले 261 वर्णों में अंतिम सफेद स्थान की तलाश करने के लिए 'स्ट्रॉप्स' फ़ंक्शन का उपयोग करना है, उस स्थिति को वापस करें, और फिर उस स्थिति पर स्ट्रिंग को 'सबस्ट्र' के साथ स्लाइस करें। यदि टेक्स्ट 260 वर्णों से अधिक है तो केवल कार्य को लागू करने के लिए 'if' कथन में सभी को शामिल करना याद रखें। – RayOnAir

11

आप ऐसा कर सकता है: पर 260TH चरित्र से पहला अंतरिक्ष का पता लगाएं और का उपयोग करें कि फसल चिह्न के रूप में:

$pos = strpos($body, ' ', 260); 
if ($pos !== false) { 
    echo substr($body, 0, $pos); 
} 
+0

धन्यवाद। मेरे लिए काम किया! – Jason

0
$pos = strpos($body, $wordfind); 
echo substr($body,0, (($pos)?$pos:260)); 
113
substr($body, 0, strpos($body, ' ', 260)) 
+4

अच्छा, सबसे कॉम्पैक्ट विकल्प – Duncanmoo

+1

स्टाइलिश समाधान, लेकिन यूटीएफ -8 के बारे में क्या? –

+3

@ टोनी mb_substr() का उपयोग कर यूटीएफ -8 के लिए चाल करेगा। – behz4d

0

$maxlength = 50; 
substr($name, 0, ($spos = strpos($name, ' ', $lcount = count($name) > $maxlength ? $lcount : $maxlength)) ? $spos : $lcount); 
:

मैं इस समाधान का उपयोग

या इनलाइन:

substr($name, 0, ($spos = strpos($name, ' ', $lcount = count($name) > 50 ? $lcount : 50)) ? $spos : $lcount); 
0
function substr_word($body,$maxlength){ 
    if (strlen($body)<$maxlength) return $body; 
    $body = substr($body, 0, $maxlength); 
    $rpos = strrpos($body,' '); 
    if ($rpos>0) $body = substr($body, 0, $rpos); 
    return $body; 
} 
-2
public function Strip_text($data, $size, $lastString = ""){ 
    $data = strip_tags($data);   
    if(mb_strlen($data, 'utf-8') > $size){ 
     $result = mb_substr($data,0,mb_strpos($data,' ',$size,'utf-8'),'utf-8'); 
      if(mb_strlen($result, 'utf-8') <= 0){ 
      $result = mb_substr($data,0,$size,'utf-8'); 
      $result = mb_substr($result, 0, mb_strrpos($result, ' ','utf-8'),'utf-8');;   
     } 
     if(strlen($lastString) > 0) { 
      $result .= $lastString; 
     } 
    }else{ 
    $result = $data; 
    } 
    return $result; 
} 

funtion Strip_text में स्ट्रिंग दर्रा ("HTML टैग के साथ या HTML टैग के बिना लंबे समय तक पाठ", 15) फिर इस समारोह पहले वापस आ जाएगी एचटीएमएल टैग के बिना एचटीएमएल स्ट्रिंग से 15 चरित्र। जब 15 वर्ण से कम स्ट्रिंग करते हैं तो पूर्ण स्ट्रिंग को दूसरी बार लौटाएं, यह 15 वर्ण को $ LastString पैरामीटर स्ट्रिंग के साथ वापस कर देगा।

उदाहरण:

Strip_text("<p>vijayDhanasekaran</p>", 5) 

परिणाम: विजय

Strip_text("<h1>vijayDhanasekaran<h1>",5,"***....") 

परिणाम: विजय *** ....

+1

थोड़ा और संदर्भ या स्पष्टीकरण उपयोगी हो सकता है: यह कैसे काम करता है, यह विधि क्यों, आदि – achedeuzot

+0

जब आप utf-8 fonts ex "तमिल" दर्ज करते हैं तो आप बिना किसी संबंधित वर्ण के सबस्ट्रिंग प्राप्त कर सकते हैं। – vijay

-1

इस समारोह का प्रयास करें ..

<?php 
/** 
* trims text to a space then adds ellipses if desired 
* @param string $input text to trim 
* @param int $length in characters to trim to 
* @param bool $ellipses if ellipses (...) are to be added 
* @param bool $strip_html if html tags are to be stripped 
* @param bool $strip_style if css style are to be stripped 
* @return string 
*/ 
function trim_text($input, $length, $ellipses = true, $strip_tag = true,$strip_style = true) { 
    //strip tags, if desired 
    if ($strip_tag) { 
     $input = strip_tags($input); 
    } 

    //strip tags, if desired 
    if ($strip_style) { 
     $input = preg_replace('/(<[^>]+) style=".*?"/i', '$1',$input); 
    } 

    if($length=='full') 
    { 

     $trimmed_text=$input; 

    } 
    else 
    { 
     //no need to trim, already shorter than trim length 
     if (strlen($input) <= $length) { 
     return $input; 
     } 

     //find last space within length 
     $last_space = strrpos(substr($input, 0, $length), ' '); 
     $trimmed_text = substr($input, 0, $last_space); 

     //add ellipses (...) 
     if ($ellipses) { 
     $trimmed_text .= '...'; 
     }  
    } 

    return $trimmed_text; 
} 
?> 
+1

कृपया मतदाता टिप्पणी करें ... – edCoder

1

वर्डव्रैप और विस्फोट करें तो पहले सरणी तत्व आप $wr=wordwrap($text,20,':'); $strs=explode(":",$wr); $strs[0]

0

इसके बारे में क्या चाहते हैं?

/** 
* @param string $text 
* @param int $limit 
* @return string 
*/ 
public function extractUncutPhrase($text, $limit) 
{ 
    $delimiters = [',',' ']; 
    $marks = ['!','?','.']; 

    $phrase = substr($text, 0, $limit); 
    $nextSymbol = substr($text, $limit, 1); 


    // Equal to original 
    if ($phrase == $text) { 
     return $phrase; 
    } 
    // If ends with delimiter 
    if (in_array($nextSymbol, $delimiters)) { 
     return $phrase; 
    } 
    // If ends with mark 
    if (in_array($nextSymbol, $marks)) { 
     return $phrase.$nextSymbol; 
    } 

    $parts = explode(' ', $phrase); 
    array_pop($parts); 

    return implode(' ', $parts); // Additioanally you may add ' ...' here. 
} 

टेस्ट:

public function testExtractUncutPhrase() 
{ 
    $stringUtils = new StringUtils(); 

    $text = 'infant ton-gue could make of both names nothing'; 
    $phrase = 'infant'; 

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 11)); 
    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 12)); 

    $text = 'infant tongue5'; 
    $phrase = 'infant'; 

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13)); 
    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 11)); 
    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 7)); 
} 

public function testExtractUncutPhraseEndsWithDelimiter() 
{ 
    $stringUtils = new StringUtils(); 

    $text = 'infant tongue '; 
    $phrase = 'infant tongue'; 

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13)); 

    $text = 'infant tongue,'; 
    $phrase = 'infant tongue'; 

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13)); 
} 

public function testExtractUncutPhraseIsSentence() 
{ 
    $stringUtils = new StringUtils(); 

    $text = 'infant tongue!'; 
    $phrase = 'infant tongue!'; 

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 14)); 
    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 100)); 

    $text = 'infant tongue!'; 
    $phrase = 'infant tongue!'; 

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13)); 

    $text = 'infant tongue.'; 
    $phrase = 'infant tongue.'; 

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13)); 
} 
संबंधित मुद्दे

 संबंधित मुद्दे