Processing text and convert to string without removing the text format in php - arrays

I have the following text as inputText:
HEADER:This is title 1
HEADER:This is title 2
HEADER: This is title 3
I want to remove "HEADER:" and convert the text into long string and keep the text format(without remove the spaces) as sample below:
This is title 1 This is title 2 This is title 3
Assuming the length for each line is fix to 30 character (include spaces).
The following is my code.
$string = $this->input->post(inputText);
//move the string to array
$Text = array($string);
$lines = count($Text);
$x=0;
while($x<=$lines)
{
//Remove "HEADER:" Length is = 7
$newText[$x] = array(substr($Text[$x],7));
$x++;
}
$stringText = implode("\n",$newText);
echo $stringText;
But it seems this code is not working..

Hey Julie the problem was coming that the explode function was removing the white spaces in the string so
$string = str_replace(" ", "-", $string);
Now you will get your sub string with all white spaces would be replaced by -
$Texts = explode('HEADER:',$string);
foreach($Texts as $key=>$data)
{
$Texts[$key] = str_replace('-',"&nbsp",$data);
}
By this you will have a array with sub strings you required in a array
UPDATE
$stringPhp = str_replace(" ", "-", $string);
$Texts = explode('HEADER:',$string);
$TextPHP = explode('HEADER:',$string);
foreach($TextPHP as $key=>$data)
{
$TextsPHP[$key] = str_replace('-',"&nbsp",$data);
}
//U can use $TextsPHP for you php I am sure it will not give you any troubles.
for html suppose you want to print $Texts[1] element.
<input type="text" value="<?php echo $Texts[1];" />
Now you will see that in html output, those blank spaces would be present

Related

IronPDF FormField value with new line

Using IronPDF, I need to fill in a PDF Form Text Field with a multiline string. A single string works fine, but using Environnment.NewLine adds a symbol; \n and \r\n are displayed as text.
I need to avoid modifying the PDF in any way.
'Example that isn't working
Dim pdf = PdfDocument.FromFile("mypdf.pdf")
pdf.Form.Fields(0).Value = "A string with" & Environment.NewLine & "multiple lines"
Thanks in advance!
The solution I found was applying the new line in the HTML:
<div>
#{
var text = #Model.Value.Split("\n");
foreach (string line in text)
{
#line.ToString();
<br />
}
}
</div>

Add Single quotes where string contains comma in AngularJS

I have a string which contains values like
var string = A,B,C
Here I want to add single quote for each comma value, expected result should be as below
output = 'A','B','C'
My angular code is
var data = {
output : this.string.split("\',"),
}
which is giving result as
["A,B,C"]
Could anyone please help on this how can I get desired output.
I am understanding your code as
var string = "A,B,C"; // because string should be in this format.
and you just need to replace "\'," from your split function to "," which will give you an array like this
var out = string.split(",");
console.log(out);
[ 'A', 'B', 'C' ] // this is the output.
as split function searches for the given expression and split the string into array.
but if you just want to modify the string without making it in array then you can use the below trick
var out = "'" + string.replace(/,/g, "','") + "'";
console.log(out);
'A','B','C' // result as u mentioned and this is of string type.

Swift 3 replace words in String, checking against values in array

Let's say I have a variable of type String holding a single String. In this string, there are 1 or more words. I also have an array of Strings. I want to check the String against the array, looking for, and replacing words found in the array.
For instance, the word "able" is in the array. The word "able" is also in the String variable. I want the word able replaced with the "+" character.
I've tried like this:
//stopWords = array of Strings / keywords = String variable
for words in stopWords {
keywordsFormatted = keywords.replacingOccurrences(of: words, with: "+", options: .regularExpression, range: nil)
}
and that doesn't change anything. I've also tried in a while loop, and a few other ways that I don't even care to recall right now lol. Any help is appreciated
Given
let stopWords = ["red", "green", "blue"]
let keywords = "The sky is blue not green"
let's create a set
let stopWordsSet = Set(stopWords)
and finally let's solve the problem
let result = keywords
.components(separatedBy: " ")
.map { stopWordsSet.contains($0) ? "+" : $0 }
.joined(separator: " ")
Oh... and lets test it
print(result) // The sky is + not +
Please note this solution is case sensitive. Furthermore it will not work is words in keywords are not delimited by spaces. E.g. "The sky is blue, not green" will not work properly because of the ,.
Addendum from Adrian:
If you want to update the textField, just use didSet, like so:
var keywords = "The sky is blue not green" {
didSet {
// if keywords is not empty, set the myTextField.text to keywords
if !keywords.characters.isEmpty {
myTextField.text = keywords
}
}
}

Capitalize a cyrillic strings with JavaScript

I'm making an AngularJS filter which capitalizes each word's first letter.
It works well with a-zA-Z letters, but in my case I use also cyrillic characters and I would like to make it work.
var strLatin = "this is some string";
var strCyrillic = "това е някакъв низ";
var newLatinStr = strLatin.replace(/\b[\wа-яА-Я]/g, function(l){
return l.toUpperCase();
});
var newCyrillicStr = strCyrillic.replace(/\b[\wа-яА-Я]/g, function(l){
return l.toUpperCase();
});
Here I got some CodePen example: http://codepen.io/brankoleone/pen/GNxjRM
You need a custom word boundary that you may build using groupings:
var strLatin = "this is some string";
var strCyrillic = "това е някакъв низ";
var block = "\\w\\u0400-\\u04FF";
var rx = new RegExp("([^" + block + "]|^)([" + block + "])", "g");
var newLatinStr = strLatin.replace(rx, function($0, $1, $2){
return $1+$2.toUpperCase();
});
console.log(newLatinStr);
var newCyrillicStr = strCyrillic.replace(rx, function($0, $1, $2){
return $1+$2.toUpperCase();
});
console.log(newCyrillicStr);
Details:
The block contains all ASCII letters, digits and underscore and all basic Cyrillic chars from the basic Cyrillic range (if you need more, see Cyrillic script in Unicode ranges Wiki article and update the regex accordingly), perhaps, you just want to match Russian with А-ЯЁёа-я, then use var block = "\\wА-ЯЁёа-я
The final regex matches and captures into Group 1 any char other than the one defined in the block or start of string, and then matches and captures into Group 2 any char defined in the block.
If you use Lodash, you can use _.startCase instead of your own implementation (they do it by splitting the string into words, capitalizing the 1st character of each word and then joining them back together)
Try it:
function capitalizer(string) {
return string.split(/\s/).map(function(item){
return (item.charAt(0).toUpperCase() + item.slice(1))
}).join(' ')
}
Example

Need help parsing results from ldap to csv

I am trying to create a script to generate a csv file with the results of some ldap queries using Net::LDAP but I'm having troubles skipping incomplete lines if one element of the #attributes array is blank.
my #attributes = ('cn', 'mail', 'telephoneNumber');
So for example, if a user has no mail listed, or no telephoneNumber listed, then it should skip the hold field instead of returning:
"Foo Bar",, # this line should be skipped since there is no mail nor telephone
"Bar Foo","bar#foo.com", # this line should be skipped too, no number listed
"John Dever","john_dever#google.com","12345657" # this one is fine, has all values
My loop right now is looking like this:
# Now dump all found entries
while (my $entry = $mesg->shift_entry()){
# Retrieve each fields value and print it
# if attr is multivalued, separate each value
my $current_line = ""; # prepare fresh line
foreach my $a (#attributes) {
if ($entry->exists($a)) {
my $attr = $entry->get_value($a, 'asref' => 1);
my #values = #$attr;
my $val_str = "";
if (!$singleval) {
# retrieve all values and separate them via $mvsep
foreach my $val (#values) {
if ($val eq "") { print "empty"; }
$val_str = "$val_str$val$mvsep"; # add all values to field
}
$val_str =~ s/\Q$mvsep\E$//; # eat last MV-Separator
} else {
$val_str = shift(#values); # user wants only the first value
}
$current_line .= $fieldquot.$val_str.$fieldquot; # add field data to current line
}
$current_line .= $fieldsep; # close field and add to current line
}
$current_line =~ s/\Q$fieldsep\E$//; # eat last $fieldsep
print "$current_line\n"; # print line
}
I have tried code like :
if ($attr == "") { next; }
if (length($attr) == 0) { next; }
and several others without any luck. I also tried simple if () { print "isempty"; } debug tests and its not working. Im not exacly sure how could I do this.
I appreciate any help or pointers you could give me on what am I doing wrong.
Thanks a lot in advance for your help.
UPDATE:
Per chaos request:
my $singleval = 0;
A sample run for this program would return:
Jonathan Hill,Johnathan_Hill#example.com,7883
John Williams,John_Williams#example.com,3453
Template OAP,,
Test Account,,
Template Contracts,,
So what I want to do is to skip all the lines that are missing a field, either email or extension number.
Label your while loop:
Record: while (my $entry = $mesg->shift_entry()){
and use:
next Record;
Your problem is that your next is associated with your foreach. Using the label avoids that.
By the way, $attr == '', though it will work in this case, is bad logic; in perl, == is a numeric comparison. String comparison would be $attr eq ''. Though I'd just use next Record unless $attr.

Resources