472,951 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

Using PHP to add a word to a txt file every X charachters

Hi,

Could PHP be used to take a txt file (or set of txt files) and add a
string of characters every X number of words or characters?

Say a txt file with 50,000 characters/5,000 words how would you go
about adding a string of characters every 5,000 characters or 500
words.

To improve on this I'd want to if using characters as the guide to use
a space or better yet a line break as the point to add the string of
characters. So 5,000 characters to the nearest line break.

Thanks

David
--
WordPress Themes with AdSense ads
http://www.morearnings.com/category/wordpress-themes/
AdSense Tips http://www.morearnings.com/2006/05/08/adsense-revenue/
Oct 24 '06 #1
2 2139
Rik
David wrote:
Hi,

Could PHP be used to take a txt file (or set of txt files) and add a
string of characters every X number of words or characters?
$text = file_get_contents('/path/to/text.txt');
$text = chunk_split($text,5000,$string_to_add);
Say a txt file with 50,000 characters/5,000 words how would you go
about adding a string of characters every 5,000 characters or 500
words.
For characters it's easy, see above.
For words, it's a little bit harder. One could fiddle around with
str_word_count(), but I would not think that the best solution.

If it does not have to be an exact:
preg_match_all('/(?:(?:^|\W*)\w*){0,500}/s',$text,$matches);
$text = implode($matches[0],$string_to_add);
To improve on this I'd want to if using characters as the guide to use
a space or better yet a line break as the point to add the string of
characters. So 5,000 characters to the nearest line break.

********** TRY 1 *****************************************
/* settings */
$string_to_add = 'Hey, this is added!!!!!!!';
$char_to_split = "\n";
$charcount_to_split = 200;

/* match char_to_split */
$char_to_split = preg_quote($char_to_split);
preg_match_all('/'.$char_to_split .'/',$text,$matches,PREG_OFFSET_CAPTURE);

/* add difference to desired position, and which occurance */
$available_line_breaks = $matches[0];
function diffs(&$value,$key,$number){
$occ = round($value[1]/$number,0);
$value['occ'] = $occ;
$value['diff'] = abs($value[1] - ($occ * $number));
}
array_walk($available_line_breaks,'diffs',$charcou nt_to_split);

/* determine which line-break is closest */
$closest = array();
function closest(&$value,$key,&$closest){
if(!isset($closest[$value['occ']]) || $closest[$value['occ']]['diff'] >
$value['diff']){
$closest[$value['occ']] = array('diff' =$value['diff'],'offset' =>
$value[1]);
}
}
array_walk($available_line_breaks,'closest',&$clos est);
array_walk($closest, create_function('&$a','$a = $a["offset"];'));

/* this code means that if there are no available line-breaks around, there
will be no value. To illustrate: */
$not_set =
array_diff(range(1,floor(strlen($text)/$charcount_to_split)),array_keys($cl
osest));
echo "For the following repeats of $charcount_to_split, no linebreaks were
found:".implode(',',$not_set);

/* you could search for a word-boundary (\W) in that region, I've left that
out */

/* Let's add the string, form last to first, otherwise our offset is off...
*/
krsort($closest);
foreach($closest as $target){
$text =
substr_replace($text,$string_to_add,$target+strlen ($char_to_split),0);
}
************************************************** *****
But offcourse, this is bullsh*t.

********** TRY 2 *****************************************
$text = text to adapt.
$string = string to add.
$count = preferred number of characters.
$split = string to split on.
$variance = the number of characters to search left and right.

function replace_text_several_times($text,$insert,$count,$s plit,$variance =
50){
$split = preg_quote($split,'/');
$regex =
'/(.{'.($count-$variance).','.($count+$variance).'})('.$split.')/si';
return preg_replace($regex,'$1$2'.$insert,$text);
}

The code above will not be near the exact number of characters, but will
nevertheless repeat the string as often as you like provided your $split
occurs.

--
Rik Wasmus
Oct 25 '06 #2
On Wed, 25 Oct 2006 02:31:19 +0200, "Rik" <lu************@hotmail.com>
wrote:
>The code above will not be near the exact number of characters, but will
nevertheless repeat the string as often as you like provided your $split
occurs.
Thanks for the info Rik, looks like the sort of stuff I was after,
will see if I can introduce it into a script I'm working with.

Thanks again.

David
--
SEO Tutorial http://www.seo-gold.com/tutorial/
More Earnings Blog http://www.morearnings.com/
Oct 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Amitava Sengupta | last post by:
Is there any issue in using COM component in web applications. The problem I'm facing are 1. Though the dll is using non-static member variables which are being instantiated on each call,...
4
by: Marcel Saucier | last post by:
Is that possible to create the body of a static (or fix) report using Word, saving that report as a RTF file and then loading that file into a RichText Box: Example, with Word, I create the...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
0
by: funeeldy | last post by:
I need to locate a particular table in a document. I cannot hardcode the table number since it could be different in every doc. I do have some header text that comes right before it consistently,...
2
by: jordanp | last post by:
Hello, I'm having a little trouble here and I'm hoping that somebody might be able to help me out (win32 console program). First off, I know that I can use class function inside of my struct as...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
5
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.