ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
# required output (Any word longer than 6 letters is clipped to its first 4 letters)
# "this is a group of words and one is very looo one"
?>
What I really want to do is something like
$str = preg_replace($pattern,substr("$1",0,4),$str);
or
$str = preg_replace($pattern,"$1[0]$1[1]$1[2]$1[3]",$str);
i.e. get the substring of the substring - if you get my meaning
One really ugly way to do it might be split the string into an array (using spaces as the delimiter), then loop through the array, clipping any words longer than 6 characters to the first 4, then rebuilding the string (I think you can do a "join" and have the spaces be put back in their proper positions...)
Like I said, ugly, but relatively easy to write Don't know if this helps you at all or not.
I think the loop would work, and you could rebuild the string as you clip/extract the orig words in the array, so you only need 1 pass through the array.
It wouldn't take long unless you had a string with a ridiculous num of words in it.
I don't think it's possible to do a preg_replace trimming a match down to a given number of chars. Using substr in the preg_replace won't work because substr cannot handle regex.
You can do as above mentioned, split the string into an array of words, replace long words and build the array back to a string.
However, you can just search for long word matches, and replace them with directly in the first string without splitting and building it back. I also made a little change, long woooooords are not replaced with "wooo" but "wo...ds".
PHP Code:
<?php //Define a test string with long words $phrase = "This is a phrase with a very looooooooooooooooong word and thhhhiiiiiiiiissssssss is your testinggggggg striiiiiiingggggg!";
//Define search pattern $pattern = "/(\S{7,})/";
//Perform regular expression match preg_match_all ($pattern, $phrase, $matches);
//Loop through each match and replace with substr foreach ($matches[1] as $longword) $phrase = str_replace ($longword, substr ($longword, 0, 2) . '..' . substr ($longword, -2), $phrase);
echo $phrase; ?>
Output:
Code:
This is a phrase with a very lo..ng word and th..ss is your te..gg st..g!
You could also use unset to free some memory if you care about it. It could help if you have thousands of matches for long words.
I'm using for a time now the unset function very much, specially when I have to deal with a lot of data during a script run.
If you don't have to make the above script work with very large posts that contain many, many loooong words, you can skip it. With every foreach loop, the array of long word matches is going to be smaller by one element, until it's empty.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.