Related Content
Display A Dynamicly Highlighted String With PHP
This function might be of limited use, but it can create some neat effects in your titles. It works by splitting a string into little bits using the spaces and then puts it back together again into two sections. The first section will be normal, but the second section will be wrapped in a span element.
PHP Random String Function
I was testing a string manipulation function today (which I will post some other time) and I wanted to create a random string of characters that I could feed into it, so I came up with the function below. I thought it was a neat use of the rand() and chr() PHP functions, so here it is.
PHP Paragraph Regular Expression
I quite often find the need to extract a section of text from the beginning of a blog post or similar to be used as the excerpt. I normally use a function that will count the number of whole words available and return the string containing those words.
Palindromes In PHP
Richard Wiseman is a psychologist, magician, and author who runs a little blog over at http://richardwiseman.wordpress.com/. His blog talks about all sorts of things, but every Friday he posts a little puzzle that you can have a go at solving.
Format A List Of Items In PHP
It is usual when writing a list of items to separate each item with a comma, except the last two items, which are separated with the word "and". I recently needed to implement a function that took a string and converted it into a list of this type so I thought I would expand on it and post it here.
Extract Keywords From A Text String With PHP
A common issue I have come across in the past is that I have a CMS system, or an old copy of Wordpress, and I need to create a set of keywords to be used in the meta keywords field. To solve this I put together a simple function that runs through a string and picks out the most commonly used words in that list as an array.
Comments
Submitted by DV on Tue, 08/12/2008 - 11:54
PermalinkSubmitted by Adrian on Wed, 08/27/2008 - 20:49
PermalinkSubmitted by Viet Trang on Wed, 03/25/2009 - 04:48
PermalinkSave a bit of processor time by doing this:
Submitted by Shogobg on Wed, 07/13/2011 - 22:42
PermalinkSubmitted by Vikas Kumar on Tue, 10/22/2013 - 11:24
PermalinkSubmitted by Kay on Mon, 10/24/2016 - 01:35
PermalinkSubmitted by Kade on Wed, 02/01/2017 - 02:01
PermalinkSubmitted by Cameron Green on Sat, 02/18/2017 - 04:21
PermalinkSubmitted by Web Developer Php on Thu, 08/10/2017 - 05:50
PermalinkThanks for this, it saved my skin
Submitted by Kay on Tue, 09/25/2018 - 18:53
PermalinkGreat.! - Still working, tested with PHP7.2
// UTF8 compatibility ยป add "mb_" before "strlen" for UTF8 compatibility
function substrwords($text, $maxchar, $end=' ...') {
if (mb_strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = mb_strlen($output)+mb_strlen($words[$i]);
if ($length > $maxchar) {
break;
}
else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
}
else {
$output = $text;
}
return $output;
}
Submitted by Mike Orfanidis on Mon, 11/05/2018 - 12:05
Permalink(Y)
Submitted by Anonymous on Tue, 05/07/2019 - 17:08
PermalinkThis is so coolaborative, then I show an more easy way:
function substrwords($text, $maxchar) {
$output = "";
$output .= substr($text, 0, $maxchar);
$output .= " ...";
return $output;
}
Maybe can validate if $text containt a string, but that is correspondence.
If a call a function is better make a condition first, like that:
if(empty($_POST['string_value_container'])) {
/* Or the process that you consider necessary, or not the process then use ! to go on else directly */
echo "Not contain an valid char/string"
}
else {
echo substrwords($string,30);
}
Submitted by Chris Silva on Sun, 01/10/2021 - 08:48
PermalinkThx for this little handy function.
Submitted by Andreas on Tue, 01/12/2021 - 16:28
PermalinkAdd new comment