Im planning to remove all the Next line at the beginning of the string,
i tried using. str_replace("\n",null,$resultContent) it gives me the result that all Next line are removed.
Example. i need to remove the next line at the beginning of this string
"
String here
String following."
I need to delete the next line at the beginning
Please refer this page .
http://www.w3schools.com/php/func_string_trim.asp
use ltrim($resultContent,"\n") to remove all new line chars from starting of string.
Just explode and take the first result
Do not forget to do some test : if !is_array() .....
$x = explode("\n", $resultContent);
$resultContent = $x[0];
You can also use it like this:
if(startsWith($resultContent, '\n')) { // true: if string starts with '\n'
str_replace("\n",null,$resultContent);
}
Not sure whether you just want to strip blank lines, or remove everything after the \n from the first instance of a word... went with the former so hopefully this is what you're after:
$string = "String first line
string second line";
$replaced = preg_replace('/[\r\n]+/', PHP_EOL, $string);
echo $replaced;
Returns:
String first line
string second line
sounds like ltrim() is what you're looking for:
ltrim — Strip whitespace (or other characters) from the beginning of a
string!
echo $result_string = ltrim($string);
Related
i have this code:
//This line is for the input that i've looped above my code, it's a URL
$textAr[$i] = str_replace("\r\n","", $textAr[$i]);
//This is for implode purposes
$finalArray = array($textAr[$i], $m1, $m2, $m3, $m4, $m5, $m6);
//When i echo this variable, $test, the URL is in the line along with the implode arrays
$test = implode($finalArray, "***");
echo $test."\n";
//This is for writing into my text file
fwrite($sitesTxt, implode($finalArray, "***")."\n");
I'm having the kind of error where after i input a 3 URLs, the first and second URL has new line after i write in the file, but the last URL I've input is in line along with the imploded arrays. I've even trimmed the $textArr, but i keep getting the new lines.
Expected output:
https://docs.google.com***false***false***false***false***false***false***
https://stackoverflow.com***false***false***false***false***false***false***
https://stackexchange.com***false***false***false***false***false***false***
Output i'm getting at the txt file:
https://docs.google.com
***false***false***false***false***false***false***
https://stackoverflow.com
***false***false***false***false***false***false***
https://stackexchange.com***false***false***false***false***false***false***
Depending on your system, your lines may not end with a \r\n combination, but perhaps just \r.
I suggest either change str_replace to:
$textAr[$i] = str_replace(array("\r","\n"),"", $textAr[$i]);
Or, change the array:
$finalArray = array(trim($textAr[$i]), $m1, $m2, $m3, $m4, $m5, $m6);
Incidentally, although it will work, your implode parameters are reversed:
$test = implode("***", $finalArray);
You should use PHP_EOL constant for 'break-line' character. Because the break-line character in DOS is \r\n, but in *nix, it's just \n.
So, you replace the first line to
$textAr[$i] = str_replace(PHP_EOL, '', $textAr[$i]);
I have a peragraph with some new lines :
First line
Second line
Third line
And this is the last line
I want to get the second line from the above peragraph.
So the result I want should be :
"Second line"
I have tried the following script with preg_match_all() function but I don't know why it's not working.
<?php
$pera="First line
Second line
Third line
And this is the last line";
preg_match_all("#\n+{2}.*+#",$pera,$results);
print_r($results);
Do you have any idea how to get the second line from the paragraph?
Any help is much appriciated.
Thanks!
Only for the purpose demonstrated, explode is really better for performance, but if you do want/have to use regex, don't use preg_match_all. That makes it global but you don't need that so go with preg_match. Then, change the pattern:
\n{2}.*
This will match the second line including leading newline character.
https://regex101.com/r/jA3dL9/1
If you want to match w/o the newline, use a capturing group:
\n{2}(.*)
Try this:
$pera="First line
Second line
Third line
And this is the last line";
$results = explode("\n", $pera);
print_r($results[2]);
Try with:
$data = array_values(
array_filter(
explode("\r\n", $pera) // or just \n
)
);
echo $data[1]; // n°line - 1
Demo: http://3v4l.org/gpgOj
Example (file=xref.tex):
This is a example string and first line with <xref>id1</xref>then,<xref>id2</xref>and with no line breaks<xref>id3</xref>.
This is a second line which has <xref>id4</xref>
Example (file=id):
id1 eqvalue1
id2 eqvalue2
id3 eqvalue3
id4 eqvalue4
Requirement: Every unique id has a equivalent value. I need to replace that equivalent value in the place of id in each occurrences in "xref.tex" file.
Tried so far:
$xref=file("xref.tex");
$idfile=file("id");
for($y=0;$y<count($xref);$y++){
for($z=0;$z<count($idfile);$z++){
$idvalue=explode(" ",$idfile[$z])//exploding based on space charac
$id1=$idvalue[0]; //this is equivalent value of unique id
$id2=$idvalue[1]; // this is unique id
preg_match( '/<xref>(.*?)<\/xref/', $xref[$y], $match );
//getting the content between "<xref>"and "</xref>"
if($match[1]===$id2{
$xref[$y]=str_replace($match[1],$id1,$xref[$y]);}
//here first occurrence of id is replaced. how to replace
//second occurrence of id in a line as
//preg_match( '/<xref>(.*?)<\/xref/', $xref[$y], $match )
//this regex focusing on first occurrence only every time.
//???? prob here is how can i do this logic in all the occurrences
//in each line
}
}
}
Expected output:
This is a example string and first line with <xref>eqvalue1</xref>then,<xref>eqvalue2</xref>and with no line breaks<xref>eqvalue3</xref>.
This is a second line which has <xref>eqvalue4</xref>
Here is what I understand. The contents of the file xref.tex is as follows
<xref>id1</xref><xref>id2</xref><xref>id3</xref><xref>id4</xref> //line 1
<xref>id2</xref><xref>id3</xref> //line 2
<xref>id4</xref> //line 3
... and so on
First of all, you have to fix the regex. You're missing > at the end of it. It should be
/<xref>(.*?)<\/xref>/
Then you need to use preg_match_all instead of preg_match as suggested.
I've modified the code a little bit. This should also work if you have same id repeating in a single line.
$xref=file("xref.tex");
$idfile=file("id");
for($y=0;$y<count($xref);$y++)
{
preg_match_all( '/<xref>(.*?)<\/xref/', $xref[$y], $match ); //get all matches and store them in *match*
for($z=0;$z<count($idfile);$z++)
{
$idvalue=explode(" ",$idfile[$z]);
$id1=$idvalue[0];
$id2=$idvalue[1];
//Below, we're replacing all the matches in line with corresponding value. Edit: Maybe not the best way, but it will give you an idea.
foreach($match[0] as $matchItem)
$xref[$y]=str_replace($matchItem,$id1,$xref[$y]);
}
}
EDIT
You might want to check preg_replace. I think that would be a better solution.
Read the file "id" as space separated csv to an array and then use that array with preg_replace on the other file as string using file_get_contents.
Try this:
$re = "/(<xref>[^\\d]+)(\\d)(<\\/xref)/m";
$str = "This is a example string and first line with <xref>id1</xref>then,<xref>id2</xref>and with no line breaks<xref>id3</xref>. This is a second line which has <xref>id4</xref>";
$subst = "$1eqvalue$2$3";
$result = preg_replace($re, $subst, $str);
Live demo
When renumbering an array using
$arr=array_values($arr); // Renumber array
I realized that a line break is being introduced into one of the array strings, which I don't want.
My string is going from:
Property
Type
to Property
Type
In any case I am using:
$newelement = preg_replace("/[^A-Za-z0-9\s\s+]/", " ", $element);
already to remove unwanted characters prior to database insertion, so I tried to change it to:
$newelement = preg_replace("/[^A-Za-z0-9\s\s+'<br>''<br>''/n''/cr']/", " ", $element);
But there is no change, and the ?line feed/line break/carriage return is still there.
Am I doing the preg_replace call correctly?
That preg looks a bit complicated. And then you have ^ in the beginning as not A-Z... or linefeed. So you don't want to replace linefeed?
How about
$newelement = preg_replace("/[\n\r]/", "", $element);
or
$newelement = preg_replace("/[^A-Za-z ]/", "", $element);
\s also matches linefeed (\n).
This should work too:
// char(32) is whitespace
// For CR
$element = strtr($element, chr(13), chr(32));
// For LF
$element = strtr($element, chr(10), chr(32));
This thing worked for me.
preg_replace("/\r\n\r\n|\r\r|\n\n/", "<br />", $element);
It's a hack, but you can do something like this:
$email_body_string = preg_replace("/\=\r\n$/", "", $email_body_string);
The replacement says find a line that ends with an equals sign and has the standard carriage return and line feed characters afterwards. Replace those characters with nothing ("") and the equals sign will disappear. The line below it will be pulled up to join the first line.
Now, this implies that you will never have a line that ends with an equals sign, which is a risk. If you want to do it one better, check the line length where the wrap (with the equals sign) appears. It's usually about 73 characters in from the beginning of the line. Then you could say:
if (strlen(equals sign) == 73)
$email_body_string = preg_replace("/\=\r\n$/", "", $email_body_string);
I have the following string
"This is very nice. Thanks for your support."
Now I want to remove the line that contains Thanks word
you need to explode the paragraph by fullstop.
lets say your full paragraph is store in $str
$lines_arr = explode(".",$str);
now $lines_arr is an array contains numbers of lines.
now under loop you can check which lines contains "thanks" , if it is then skip it.
$string = '';
for($i=0; $i<(count($lines_arr)-1); $i++){
//with the help of strpos
if(strpos($lines_arr[$i],"Thanks") == false){
$string .= $lines_arr[$i].". ";
}
}
you can use str_replace
echo str_replace("Thanks", "", "This is very nice. Thanks for your support.");
# Output: This is very nice. for your support.
If you expect proper punctuation, you could explode() the string into an array on the full stops and delete the elements with your particular word, then merge all the elements back into a string.