Currently the only global PHP command I know is:
<?=$text_items?>
This spits:
1 item(s) - £318.75
I want to get the 318.75 value so at the moment I am trying a replace but it is not working all smoothly:
$short = $text_items;
$short = str_replace("£", "", $short);
$short = str_replace("£", "", $short);
$short = str_replace("-", "", $short);
$short = str_replace("–", "", $short);
$short = str_replace(" ", "", $short);
$short = str_replace("-", "", $short);
$short = str_replace("ITEMS", "", $short);
$short = str_replace("(", "", $short);
$short = str_replace(")", "", $short);
$short = str_replace("item(s)", "", $short);
$short = str_replace("ITEM", "", $short);
$total = #floatval(end(explode('£', html_entity_decode($text_items))));
html_entity_decode changes £ to £
end(explode('£' is giving you string after '£' character
finally floatval is valuating string to float.
# is bypassing E_STRICT error which occurs to passing constant in end() function.
Working example
Second solution is Regexp:
preg_match_all('!\d+(?:\.\d+)?!', $text_items, $result);
echo $result[1];
Working example
Related
How to avoid mobile numbers less than ten digits? My code is given below. Above ten digits are not allowed. But less than ten digits are taken. Please how to sort the numbers if they are only 10 digit ?
foreach ($phone_numbers as $key => $phone_number) {
$phone_number = ltrim($phone_number, "0");
$phone_number = substr($phone_number, -10);
$phone_number = str_replace("+", "", $phone_number);
$phone_number = str_replace(" ", "", $phone_number);
$phone_number = str_replace("'", "", $phone_number);
$phone_number = str_replace("`", "", $phone_number);
$phone_number = str_replace("\"", "", $phone_number);
$phone_number = str_replace("-", "", $phone_number);
$phone_number = str_replace("/", "", $phone_number);
$phone_number = str_replace("_", "", $phone_number);
$phone_number = str_replace("*", "", $phone_number);
$phone_number = str_replace("#", "", $phone_number);
$phone_number = str_replace(",", "", $phone_number);
$phone_number = str_replace(".", "", $phone_number);
$phone_number = str_replace("=", "", $phone_number);
$country_code = '91';
$phone_number = trim($country_code.$phone_number);
Instead of guessing what characters the user is going to enter why not use regex?
$phone_nuber_digits = preg_replace("/[^0-9]/", '', $phone_number);
$number_digits = strlen($phone_nuber_digits);
Now you can check how long is your phone number to make sure that is 10 digits
i make a text preprocess on text mining with large database,, i want make a camus data from all article on database into array, but it take to long process.
$multiMem = memory_get_usage();
$xstart = microtime(TRUE);
$word = "";
$sql = mysql_query("SELECT * FROM tbl_content");
while($data = mysql_fetch_assoc($sql)){
$word = $word."".$data['article'];
}
$preprocess = new preprocess($word);
$word= $preprocess->preprocess($word);
print_r($kata);
$xfinish = microtime(TRUE);
here is my class preprocess
class preprocess {
var $teks;
function preprocess($teks){
/*start process segmentation*/
$teks = trim($teks);
//menghapus tanda baca
$teks = str_replace("'", "", $teks);
$teks = str_replace("-", "", $teks);
$teks = str_replace(")", "", $teks);
$teks = str_replace("(", "", $teks);
$teks = str_replace("=", "", $teks);
$teks = str_replace(".", "", $teks);
$teks = str_replace(",", "", $teks);
$teks = str_replace(":", "", $teks);
$teks = str_replace(";", "", $teks);
$teks = str_replace("!", "", $teks);
$teks = str_replace("?", "", $teks);
//remove HTML tags
$teks = strip_tags($teks);
$teks = preg_replace('#<(\w+)\b.*?>.*?</\1>#si', '', $teks);
/*end proses segmentation*/
/*start case folding*/
$teks = strtolower($teks);
$teks = preg_replace('/[0-9]+/', '', $teks);
/*end case folding*/
/*start of tokenizing*/
$teks = explode(" ", $teks);
/*end of tokenizing*/
/*start of filtering*/
//stopword
$file = file_get_contents('stopword.txt', FILE_USE_INCLUDE_PATH);
$stopword = explode("\n", $file);
//remove stopword
$teks = preg_replace('/\b('.implode('|',$stopword).')\b/','',$teks);
/*end of filtering*/
/*start of stemming*/
require_once('stemming.php');
foreach($teks as $t => $value){
$teks[$t] = stemming($value);
}
/*end of stemming*/
$teks = array_filter($teks);
$teks = array_values($teks);
return $teks;
}
}
anyone have any idea to make fast process on my program? pls help
thanks for advance
Ther are a couple of things that might be improoved...
After building up the $word you could free the query result $sql and the data
$word = '';
$sql = mysql_query("SELECT * FROM tbl_content");
while($data = mysql_fetch_assoc($sql)){
$word = $word . $data['article'];
}
mysql_free_result($sql);
unset($sql, $data);
This block:
$teks = str_replace("'", "", $teks);
$teks = str_replace("-", "", $teks);
$teks = str_replace(")", "", $teks);
$teks = str_replace("(", "", $teks);
$teks = str_replace("=", "", $teks);
$teks = str_replace(".", "", $teks);
$teks = str_replace(",", "", $teks);
$teks = str_replace(":", "", $teks);
$teks = str_replace(";", "", $teks);
$teks = str_replace("!", "", $teks);
$teks = str_replace("?", "", $teks);
can be written like this:
$teks = str_replace(array('(','-',')',',','.','=',';','!','?'), '', $teks);
since you later in the code replace the numbers with a regular expression, you could add numbers in the upper str_replace call, or add the upper chars to the preg_replace
$teks = str_replace(array('0','1','2','3','4','5','6','7','8','9','(','-',')',',','.','=',';','!','?'), '', $teks);
OR
$teks = preg_replace('/[0-9,\(\)\-\=\.\,\;\!\?]+/', '', $teks);
$teks = strip_tags($teks); should be enough. If it isn'y then use just the preg_replace following, since it's doing kind of the same thing.
use file insted of the file_get_contentsfollowed by theexplodesince thefilereturns an array directly. Also there is no need to explode the $teks
$stopword = file('stopword.txt');
array_walk($stopword, function(&$item1){
$item1 = '/\b' . $item1 . '\b/';
});
$teks = preg_replace($stopword, '', $teks);
Generally don't use "" since the processor will try to evaluate the content and that takes longer.
If the stopword.txt list is not changing it is better and faster to have it in the code as an array directly then accessing the file system to read it.
Wrote my code to work with a specific .cvs format and it was working for the format of the files I had. Now the format of the .cvs file has changed.
So now it looks like everything is inserting but it has shifted over. More specifically everything has moved over one whole column to the right.
I was able to modify the code to make it move further over to the right, but I was unsuccessful at moving it to the left.
Here the part of my code that is having issues:
if($deleteSuccess == 1){
echo "Now inserting all new records";
$handle = fopen($target_file, "r");
$count = 0;
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$name = $data[0];
$correct0 = str_replace("'", "", $name);
$correct1 = str_replace("'", "", $data[1]);
$correct2 = str_replace("'", "", $data[2]);
$correct3 = str_replace("'", "", $data[3]);
$correct4 = str_replace("'", "", $data[4]);
$correct5 = str_replace("'", "", $data[5]);
$correct6 = str_replace("'", "", $data[6]);
$correct7 = str_replace("'", "", $data[7]);
$import="INSERT into DVDCatalog.testtable(`Volume Name`,`DVD Number`,`Type`,`Category`,`Date`,`Author`,`Availablity`,`Picture`) values('$name','$correct1','$correct2','$correct3','$correct4','$correct5','$correct6','$correct7')";
$count = $count + 1;
mysqli_query($conn, $import);
if(mysqli_error($conn)){
echo "<br> Problem with --> ".$import."<br>";
Thanks.
If everything has moved over 1 column to the right, then the data that used to be in $data[0], $data[1], etc. is now in $data[1], $data[2], etc. To get insert data the same as you used to, you should not be doing anything with $data[0], because that's the new field that was added to the CSV file but doesn't correspond to the old format.
So it should be:
$correct0 = str_replace("'", "", $data[1]);
$correct1 = str_replace("'", "", $data[2]);
$correct2 = str_replace("'", "", $data[3]);
$correct3 = str_replace("'", "", $data[4]);
$correct4 = str_replace("'", "", $data[5]);
$correct5 = str_replace("'", "", $data[6]);
$correct6 = str_replace("'", "", $data[7]);
$correct7 = str_replace("'", "", $data[8]);
How can I increase the performance of the following code:
$text = str_replace("A", "B", $text);
$text = str_replace("f", "F", $text);
$text = str_replace("c", "S", $text);
$text = str_replace("4", "G", $text);
//more str_replace here
Do it as one function call:
$text = str_replace(["A","f","c","4"], ["B","F","S","G"], $text);
is it possible to make this more smooth with less line of codes since i have to repeat it for every new box i need to insert it into.
$fil_namn = str_replace("5FSE_", "", $fil_url);
$fil_namn = str_replace(".pdf", "", $fil_namn);
$fil_namn = str_replace(".docx", "", $fil_namn);
$fil_namn = str_replace(".doc", "", $fil_namn);
$fil_namn = preg_replace("[_]",". ",$fil_namn);
$fil_namn = preg_replace('/^[0-9]+\. +/','', $fil_namn);
$fil_namn = preg_replace ("[AaA]","å",$fil_namn);
$fil_namn = preg_replace ("[AeA]","ä",$fil_namn);
$fil_namn = preg_replace ("[OoO]","ö",$fil_namn);
$fil_namn = preg_replace ("[aAa]","Å",$fil_namn);
$fil_namn = preg_replace ("[aEa]","Ä",$fil_namn);
$fil_namn = preg_replace ("[oOo]","ö",$fil_namn);
$fil_namn= str_replace("."," ", $fil_namn);
You could use this:
str_replace(array('5FSE_', '.pdf', '.docx', '.doc'), '', $fill_namn);
str_replace allows for arrays.
You can also do this:
$string = "Hello";
echo str_replace(array("H", "e", "l", "o"), array("A", "l", "e", "x"), $string);
This will print out Aeeex.
Another method would be to use the strtr() function:
$string = "[AaA][AeA][OoO][aAa][aEa][oOo]";
$find = array("[AaA]", "[AeA]", "[OoO]", "[aAa]", "[aEa]", "[oOo]");
$replace = array("å", "ä", "ö", "Å", "Ä", "ö");
echo strtr($string, array_combine($find, $replace));
This echoes out:
åäöÅÄö