LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2006, 06:37 PM   #1
senyahnoj
Member
 
Registered: Jul 2004
Location: Gloucestershire, UK
Distribution: Ubuntu, Debian & Gentoo
Posts: 74

Rep: Reputation: 16
php preg_replace substring of a substring [solved


I'm trying to write a regular expression which reduces the length of long words in a string. Here's how far I've got:

PHP Code:
<?
$str 
"this is a group of words and one is very looooooooooooooooooooong one";

$pattern "/(\S{7,})/";

$str preg_replace($pattern,"$1",$str);
echo 
$str;

# 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

Does anyone know how to do this?

Last edited by senyahnoj; 12-08-2006 at 11:49 AM.
 
Old 12-07-2006, 07:52 PM   #2
pnellesen
Member
 
Registered: Oct 2004
Location: Missouri, USA
Distribution: Slackware 12.2, Xubuntu 9.10
Posts: 371

Rep: Reputation: 31
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.
 
Old 12-08-2006, 01:21 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,685

Rep: Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492
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.
 
Old 12-08-2006, 07:18 AM   #4
Boby
Member
 
Registered: Feb 2004
Posts: 781

Rep: Reputation: Disabled
Wink Replace words in defined string

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 ($longwordsubstr ($longword02) . '..' 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.
PHP Code:
foreach ($matches[1] as $key => $longword)
{
   
$phrase str_replace ($longwordsubstr ($longword02) . '..' substr ($longword, -2), $phrase);
   unset (
$matches[1][$key]);
}
unset (
$matches); 
Hope it helps!
Boby

Last edited by Boby; 12-08-2006 at 07:19 AM.
 
Old 12-08-2006, 11:48 AM   #5
senyahnoj
Member
 
Registered: Jul 2004
Location: Gloucestershire, UK
Distribution: Ubuntu, Debian & Gentoo
Posts: 74

Original Poster
Rep: Reputation: 16
Thanks very much for all your suggestions, and yes you're entirely right Boby, I was planning to put the ellipsis in the middle of the w...d

I've never thought about using unset for used array elements in a foreach loop before - that's a really good tip!
 
Old 12-08-2006, 12:31 PM   #6
Boby
Member
 
Registered: Feb 2004
Posts: 781

Rep: Reputation: Disabled
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.

Boby
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
extract substring from string in C baddah Programming 6 02-02-2010 05:22 AM
replace a substring with another string in C zeppelin Programming 21 11-09-2009 10:59 PM
Need Help Extracting A Substring eur0dad Linux - General 6 09-21-2006 11:34 PM
to get a substring dimah Programming 1 06-22-2006 07:31 AM
how many occurances of a substring are they n a string linuxmandrake Programming 1 04-11-2006 03:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration