I want to get last part of url separated by blackslash
Here is my code
<?php
$str = "C:\xampp\htdocs\sanimailer\storage\mail-attachments\10819_21098672691424384960_C
opy_of_tristan_data_cleanup_version_FBL_3.2.xlsx";
echo basename($str);
?>
I want output as this
10819_21098672691424384960_Copy_of_tristan_data_cleanup_version_FBL_3.2.xlsx
But I am getting output as this
Not sure why I am getting such output .
You need to convert " to ' and then it will work:-
<?php
$str = 'C:\xampp\htdocs\sanimailer\storage\mail-attachments\10819_21098672691424384960_C
opy_of_tristan_data_cleanup_version_FBL_3.2.xlsx';
echo basename($str);
?>
Output:- https://eval.in/829073
The problem you are facing because "\10" converted to that symbol
check here:- https://eval.in/829077
Related
I am trying to convert the apostrophe in URL string using htmlentities() or htmlspecialchars() .... but it is not working for me...
I have following code:
<?php
$new = htmlspecialchars("<a href='http://abc.test.net/content/22799-mdsap-partners-with-sap’s-‘moving-experience’-initiative-in-the-uae-and-oman'>Test</a>");
echo $new;
?>
but I am getting the output from $new:
<a href='http://abc.test.net/content/22799-mdsap-partners-with-sap’s-‘moving-experience’-initiative-in-the-uae-and-oman'>Test</a>
How to convert the apostrophe and single quotes in url...
Try to use urlencode("your URL") only on the part you need (otherwise it will mess up the rest of the URL):
$new = "<a href='http://abc.test.net/content/" . urlencode("22799-mdsap-partners-with-sap’s-‘moving-experience’-initiative-in-the-uae-and-oman") . "'>Test</a>";
echo $new;
I wrote a short test code in PHP7:
<?php
$str1=' bigapple ';
echo strlen($str1);
trim($str1) ;//or trim($str1," ")
echo strlen($str1);
?>
But whenever I use trim on $str1 ,the strlen would be return 10.
Can someone tell me the reason why? I've been searching it but find nothing.
You need to store trim string to any variable or you need to print trim string as following:
echo strlen(trim($str1));
You have not assigned the trimmed value in another variable. Try as below :
<?php
$str1=' bigapple ';
echo strlen($str1).'<br>';
$str2 = trim($str1);
echo strlen($str2).'<br>';
?>
I have a long string as a single line (its too long) and I want it to break and print as a new line whenever we meet a semi colon ; and } or { for example
This is my string:
aaaaaaaaaaa;bbbbbbbbbb{ccccccccc;dddddddd;}eeeeeee{fffffff;}
I want to print it as below:
aaaaaaaaaaa;
bbbbbbbbbb{
ccccccccc;
dddddddd;
}
eeeeeeee{
fffffffffff;
}
Even if ; and } meet together I wanna break it down and print in two lines.
For example jjjjjjjjjjjjj;} might display as
jjjjjjjjjjj;
}
Please help me.
You can simply use preg_replace as
$str = 'aaaaaaaaaaa;bbbbbbbbbb{ccccccccc;dddddddd;}eeeeeee{fffffff;}';
$res = preg_replace('/(;|{|})/',"$1\n",$str);
print_r($res);
Output :
aaaaaaaaaaa;
bbbbbbbbbb{
ccccccccc;
dddddddd;
}
eeeeeeee{
fffffffffff;
}
Regex
Demo
You can use preg_replace(/([;\{\}])/, '$1<br/>', $sourceLine) for HTML output. If outputing to file, change '$1<br/>' to "$1\r\n"
You can use like the below code also :
$str = 'aaaaaaaaaaa;bbbbbbbbbb{ccccccccc;dddddddd;}eeeeeee{fffffff;}';
$arr_replace_char = array(';','{','}');
$arr_replace_by = array(';<br>','{<br>','}<br>');
echo str_replace($arr_replace_char,$arr_replace_by,$str);
Below is the code that should make the problem clear. Code that is irrelevant to the problem has been left out, e.g. fetching the contents of a url, printing the contents to a new file.
Can anyone explain why variables are jumbled except for the last item?
<?php
$f = fopen("cucina2.txt", "r");
// Read line by line until end of file
while (!feof($f)) {
// Make an array using newline as delimiter
$arrM = explode("\n",fgets($f));
//Add the word to the url address
$url = 'hhttp://www.some.com/vocab/' . $arrM[0] . '/';
//Create a text file with the word
$glossary = $arrM[0];
$glossary .= '.txt';
//Check the functions
echo "arrM is: ";
echo $arrM[0];
echo "\n";
echo "glossary is: ";
echo $glossary;
echo "\n";
echo "url is: ";
echo $url;
echo "\n";
}
?>
cucina.txt:
batticarne
battuto
bavarese
bavetta
results:
arrM is: batticarne
.txtsary is: batticarne
/rl is: hhttp://www.some.com/vocab/batticarne
arrM is: battuto
.txtsary is: battuto
/rl is: hhttp://www.some.com/vocab/battuto
arrM is: bavarese
.txtsary is: bavarese
/rl is: hhttp://www.some.com/vocab/bavarese
arrM is: bavetta
glossary is: bavetta.txt
url is: hhttp://www.some.com/vocab/bavetta/
It appears that your data file (cucina.txt) contains Windows-style line breaks ("\r\n"). When you split the input lines at \n, you are left with a trailing carriage return \r at the end of each slice.
Try this instead:
$arrM = preg_split('/[\r\n]+/',fgets($f));
Also, be aware that while (!feof($f)) is generally wrong. You should check for an EOF when you are actually reading from the file, not at the top of your loop. That way you will avoid processing empty lines.
while (!feof($f)) {
$s = fgets($f);
if (!$s) break;
$arrM = preg_split('/[\r\n]+/',$s);
:
:
}
I just ran your file. Made my own word list however.
I put just a list of stuff around me in into a text file:
style
charity
new
samsung
asus
sharpie
tape
It prints fine. Returns
arrM is: style
glossary is: style.txt
url is: hhttp://www.some.com/vocab/style/
arrM is: charity
glossary is: charity.txt
url is: hhttp://www.some.com/vocab/charity/
arrM is: new
glossary is: new.txt
url is: hhttp://www.some.com/vocab/new/
arrM is: samsung
glossary is: samsung.txt
url is: hhttp://www.some.com/vocab/samsung/
arrM is: asus
glossary is: asus.txt
url is: hhttp://www.some.com/vocab/asus/
arrM is: sharpie
glossary is: sharpie.txt
url is: hhttp://www.some.com/vocab/sharpie/
arrM is: tape
glossary is: tape.txt
url is: hhttp://www.some.com/vocab/tape/
Your issue could be coming from using explode. If the file your reading from is already words or series of words denoted from one another by line returns you don't need to explode the string. Just read the file line by line until you reach the end.
<?php
//create variable to iterate though the array until it is out of values.
$itter = 0;
$f = fopen("WordsList.txt", "r");
// Read line by line until end of file
while (!feof($f)) {
//read the text file line by line.
$line=fgets($f);
//Removed any whitespace left at the end of the line or whitespace generated from returning.
//Trim can be used to trim any character from a string though.
$line=trim($line);
//Add the line to the array.
$arrM[]=$line;
//Add the word to the url address
$url = 'http://www.some.com/vocab/' . $arrM[$itter] . '/';
//Create a text file with the word
$glossary = $arrM[$itter];
$glossary .= '.txt';
//Check the functions
echo "arrM is: ";
echo $arrM[$itter];
echo "<br />";
echo "glossary is: ";
echo $glossary;
echo "<br />";
echo "url is: ";
echo $url;
echo "<br />";
$itter++;
}
//After you read from a file always a food idea to close it/dump it from memory.
fclose($f);
?>
Try that block of code. It uses trim to fgets to read line by line. Then trims any white space from the end of the line. Every time a line is pulled out by fgets it trims and sets that line equal to the variable $line. Then as each line is read and stripped it takes that line and adds it to your array.
The variable $itter then echos back the array starting at zero until it reaches the end of the number of lines found in the file/array generated from the file.
I have the following line in my code which displays my output in 6 characters with leading zeros.
$formatted_value = sprintf("%06d", $phpPartHrsMls);
I want to replace the leading zeros with spaces. Have tried all the examples found by searching this site and others and cannot figure it out.
Here are some I have tried:
$formatted_value = sprintf("%6s", $phpPartHrsMls);
$formatted_value = printf("[%6s]\n", $phpPartHrsMls); // right-justification with spaces
In the browser, spaces will always be collapsed.
Try:
<pre><?php echo $formatted_value; ?></pre>
And once you're satisfied with that, take a look at the CSS white-space:pre-wrap - a very useful property!
This will align the left with spaces:
$formatted_value = sprintf("%6s", $phpPartHrsMls);
echo "<pre>" . $formatted_value . "</pre>";
This will align the right with spaces:
$formatted_value = sprintf("%-6s", $phpPartHrsMls);
echo "<pre>" . $formatted_value . "</pre>";
If you want to print only six digits, and others to remove:
$formatted_value = sprintf("%-6.6s", $phpPartHrsMls);
echo "<pre>" . $formatted_value . "</pre>";
One more thing, the browser will generally ignore the spaces, so it's better to wrap your output in <pre> tag.
Changing leading zeroes to leading spaces:
$formatted_value = sprintf("%' 6s", $phpPartHrsMls);
Try str_pad.
str_pad($phpPartHrsMls, 6, " ", STR_PAD_LEFT);
you use %06d please try some larger number .
your code can some thing like below try :
printf('%20.2f', $phpPartHrsMls);
and you can use for space on your html .