473,785 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2189
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_conten ts('/path/to/text.txt');
$text = chunk_split($te xt,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,$match es);
$text = implode($matche s[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_s plit = 200;

/* match char_to_split */
$char_to_split = preg_quote($cha r_to_split);
preg_match_all( '/'.$char_to_spli t .'/',$text,$matche s,PREG_OFFSET_C APTURE);

/* 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($ava ilable_line_bre aks,'diffs',$ch arcount_to_spli t);

/* determine which line-break is closest */
$closest = array();
function closest(&$value ,$key,&$closest ){
if(!isset($clos est[$value['occ']]) || $closest[$value['occ']]['diff'] >
$value['diff']){
$closest[$value['occ']] = array('diff' =$value['diff'],'offset' =>
$value[1]);
}
}
array_walk($ava ilable_line_bre aks,'closest',& $closest);
array_walk($clo sest, 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(rang e(1,floor(strle n($text)/$charcount_to_s plit)),array_ke ys($cl
osest));
echo "For the following repeats of $charcount_to_s plit, 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($closes t as $target){
$text =
substr_replace( $text,$string_t o_add,$target+s trlen($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_se veral_times($te xt,$insert,$cou nt,$split,$vari ance =
50){
$split = preg_quote($spl it,'/');
$regex =
'/(.{'.($count-$variance).','. ($count+$varian ce).'})('.$spli t.')/si';
return preg_replace($r egex,'$1$2'.$in sert,$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
1394
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, Whenever I'm calling the (.aspx) pages using the dll more than once simultaneously, both results in garbage. When called once it is perfect in result. 2. The memory is not being released in web application of the dll even after finishing properly with...
4
1796
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 following report (using fonts size and others minimum font features): PRODUCT: +++++++++++++++++ SALES.................########.## Provincial taxes....########.## Federal taxes.......########.##
89
6079
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 used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
0
4015
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, though. So, I tried to find that text, and then move to the next table. Then I need to add a row to the table which already has 4 columns. I need to maintain the old content of the table too. I think I am pretty close, but having a little trouble...
2
1838
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 a struct object...but my issue that I'm having is that my class function is set up so that it sets 3 variables... Example of my class object (has the getter(), and setter() functions below this, but I won't display that here):
0
2051
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 word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file.
0
1926
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 PreviousToken(Previous word) and frequency and probability From text file and put each one in cell in table For example if the text file content is "Every man has a price. Every woman has a price." First Token(word) is "Every" PreviousToken(Previous...
5
2540
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 PreviousToken(Previous word)) from multube files and get frequency of both. it can get for single word but double word give error line 50, in most_frequant_word word1+= ' ' + word_list IndexError: list index out of range import...
83
4236
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 it too? Once I saw a website comparing Prototype to Java and jQuery to Ruby... but now that I read more and more about Prototype, it is said that Prototype actually came from Ruby on Rails development and the creator of Prototype created it...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.