String padding for a text template - php

I'm creating a PDF file from a txt-template with tcpdf ([Example 8][1]). The txt-template looks like this:
SALUTATION
FIRSTNAME LASTNAME
STREET CURRENTDATE
SOMEMOREINFORMATION MYWEBSITE
I replace those markers with the correct value. So that it would look like this:
Mr.
John Doe
Downingstreet 10 14th May, 2010
john#doe.com www.stackoverflow.com
In this example, when I replace the values, the indention of the date is dependent on the length of the street name (which I don't want). I could solve this issue with str_pad but the problem is, I normally use three columns and there are lines which only have content in col1 and col3 as in the last line. How can I solve that problem? Is there something like the "overwrite" function in Word, that when you write, the text just gets overwritten?
Thanks in advance.

Count street's string length and then add/remove left padding of date.

You can use sprintf, e.g.
function something($street, $currentDate, $foo) {
$s = sprintf('%-20s %-18s %s',
$street,
$currentDate,
$foo
);
return $s;
}
echo something('streetA', '14th May, 2010', 'lalala'), "\n";
echo something('Downingstreet 10', '14th May, 2010', 'lalala'), "\n";
echo something('abcdefghijklmnopqrstuvwxyz 10', '14th May, 2010', 'lalala'), "\n";
prints
streetA 14th May, 2010 lalala
Downingstreet 10 14th May, 2010 lalala
abcdefghijklmnopqrstuvwxyz 10 14th May, 2010 lalala
(as you can see from the third line the width specification is the minimum length, so you might have to use something like substr())

I presume you are just str_replace()'ing the placeholders with their values?
$streetPlaceHolder = 'STREET ';
$streetReplacement = str_pad('Downingstreet 10', strlen($streetPlaceHolder));
$template = str_replace($streetPlaceHolder, $streetReplacement, $template);
Presumably you will run into the same problem with SOMEMOREINFORMATION. This same solution can be used.
I realize you said str_pad was not an ideal solution for you. However, I do not understand why, even if you extend this to three columns. You can still get by with this method.

Related

extract value from text file using php

I'm trying to extract the price 44,380.86 between date and # using preg_match_all() from the following line.One more thing is date Jan 1, 2015 will be dynamic.Can someone tell me how to complete it?
start on Jan 1, 2015 44,380.86 # of count: 15 tc
You can use this regex (regex explanation):
start on\s[A-Za-z]+\s[1-9]+,\s[0-9]+\s+(.*?)\s+#
Example Code:
<?php
preg_match_all(
"/start on\s[A-Za-z]+\s[1-9]+,\s[0-9]+\s+(.*?)\s+#/",
"start on Jan 1, 2015 44,380.86 # of count: 15 tc",
$matches
);
var_dump($matches);
I think this should be work for your problem with other changes:
preg_match_all("(\S+(?:\s\S+)*?)","Your string",$matches);
for your question you can use:
preg_match_all("(\S+(?:\s\S+)*?)","start on Jan 1, 2015 44,380.86 # of count: 15 tc",$matches);
echo $matches[5];
this regex parse your string with spaces, so when your string change, just you can edit index of $matches from 5 to what you want

cut string to get linux time

I have this text:
DESCRIPTION:When: ‎יום שלישי 28 אוקטובר 2014 10:00 עד 11
I would like to cut the text in order to convert the dat to linux time date
how do I get the 28 (day) , אוקטובר (month), and 2014 (year), 10:00 (time) with regular expression ?
Is there any better way?
you can try to extract all numeric value using preg_match then concat as per your need.
preg_match_all('!\d+!', $yourString, $matches);
echo $dateTime = $matches[0][2].':'.$matches[0][3].' '.$matches[0][0].'-'.$matches[0][4].'-'.$matches[0][1];
echo '<pre>';
print_r($matches[0]);
exit;

Extract certain text from a string with regex

I tried to extract a coded string from a string, for instance,
$string = 'Louise Bourgeois and Tracey Emin: Do Not Abandon Me [date]Until 31 August 2011[ /date ]';
$description = preg_replace('/\[(?: |\s)*([date]+)(?: |\s)*\](.*?)\[(?: |\s)*([\/date]+)(?: |\s)*\]/is', '',$string);
$date = preg_replace('/\[(?: |\s)*([date]+)(?: |\s)*\](.*?)\[(?: |\s)*([\/date]+)(?: |\s)*\]/is', '$3',$string);
echo $date;
result:
Louise Bourgeois and Tracey Emin: Do Not Abandon Me /date
intended result:
Until 31 August 2011
I got the $description right but I can't get the [date] right. Any ideas?
I think a rather simpler form would do:
#.*?\[\s*?date\s*?\](.*)\[\s*?/date\s*?\].*#
for instance?
([date]+)
This is going to look for one-or-more sequences of letters containing d, a, t, and/or e. [] are regex metacharacters for character classes and will not treated as literal characters for matching purposes. You'd probably want:
(\[date\]) and (\[\/date\])
to properly match those opening/closing "tags".

Regex to extract timestamp from a text in PHP

I have the following bit of text (or some similar variation of it):
Recurring Event
First start: 2010-09-16 17:00:00 EDT
Duration: 4800
Event Status: confirmed
I need to select the timestamp of the "First Start" field and the duration.
Normally I would split the string at the colons, but since the timestamp contains them, it sort of becomes problematic. I'm not so good with regular expressions, but does anyone know one that will be able to select the two bits of data I need?
cheers,
Mike
Assuming the format stays this way you can search for ": ", i.e. a colon followed by a space. The string following this would be your data.
For a simple non-regex solution, find the first : with strpos($input, ":"), then the rest of the line will have the value.
$content = '
Recurring Event
First start: 2010-09-16 17:00:00 EDT
Duration: 4800
Event Status: confirmed
';
$contentArray = explode('EDT' , $content);
$head = trim($content[0]);
//$head will contain 'Recurring Event First start:2010-09-16 17:00:00 '
$headArray = explode(' ' , $head);
$timeStamp = trim(end($headArray)); //which will have 17:00:00
You can do:
if(preg_match('/First start: ([-\d :]+)/',$input,$m)) {
$timeStamp = trim($m[1]);
}else{
$timeStamp = '';
}
if(preg_match('/Duration: (\d+)/',$input,$m)) {
$duration = $m[1];
}else{
$duration = '';
}

PHP: How to find the beginning and end of a substring in a string?

This is the content of one mysql table field:
Flash LEDs: 0.5W
LED lamps: 5mm
Low Powers: 0.06W, 0.2W
Remarks(1): this is remark1
----------
Accessories: Light Engine
Lifestyle Lights: Ambion, Crane Fun
Office Lights: OL-Deluxe Series
Street Lights: Dolphin
Retrofits: SL-10A, SL-60A
Remarks(2): this is remark2
----------
Infrared Receiver Module: High Data Rate Short Burst
Optical Sensors: Ambient Light Sensor, Proximity Sensor, RGB Color Sensor
Photo Coupler: Transistor
Remarks(3): this is remark3
----------
Display: Dot Matrix
Remarks(4): this is remark4
Now, I want to read the remarks and store them in a variable. Remarks(1), Remarks(2), etc. are fixed. 'this is remark1', etc. come from form input fields, so they are flexible.
Basically what I need is: Read everything between 'Remarks(1):' and '--------' and save it in a variable.
Thanks for your help.
You can use regex:
preg_match_all("~Remarks\(([^)]+)\):([^\n]+)~", $str, $m);
As seen on ideone.
The regex will put X in match group 1, Y in match group 2 (Remarks(X): Y)
This would be a job for regular expressions, which allow you to match on exactly the kinds of rules your requirements express. Here is a tutorial for you.
Use preg function for this or otherwise you can explode and implode function to get correct result. Don't Use Substring it may not provide correction.
Example of Implode and Explode Function for your query string :
$sdr = "Remarks(4): this is remark4";
$sdr1 = explode(":",$sdr);
$frst = $sdr1[0];
$sdr2 = array_shift($sdr1);
$secnd = implode(" ", $sdr1);
echo "First String - ".$frst;
echo "<br>";
echo "Second String - ".$secnd;
echo "<br>";
Your Answer :
First String - Remarks(4)
Second String - this is remark4

Categories