Hi i am tying to get clean date but there is some thing wrong.
Here is my code.
$in = 'Kb 06/11/2001';
$result = preg_replace("/[^0-9]+/", "", $in);
echo $result;
Output is
06112001
i need output 06/11/2001
Match the slash as well to get the clean date /[^0-9\/]+/
$result = preg_replace("/[^0-9\/]+/", "", $in);
You could also make use of simple array functions in PHP. [A regex alternative]
<?php
$in = 'Kb 06/11/2001';
echo $arr= array_pop(explode(' ',$in)); //"prints" 06/11/2001
Demo
The code explodes your text by space and then pops your last element which is your date.
Related
I want to remove only country code not slash from string .
suppose: phone number is +1-555-555-5555 and i want like 555-555-5555
My code is
<?php
$sPhoneNumber =get_post_meta($post_id, '_vfb_field-23', true);
echo $result = preg_replace("/[^0-9]/", "", $sPhoneNumber);
?>
it given me output like 15555555555 and i want 555-555-5555
Use the following regex substitution:
$result = preg_replace("/^\+\d+-/", "", $sPhoneNumber);
It will remove 1+ digits and a - after it from the start of a string.
Using this solution, you will avoid changing strings that do not start with + followed with digits.
Instead of \d+ you may specify the country codes you need to remove. Say, you need to remove 22 and 48 codes:
$result = preg_replace("/^\+(?:48|22)-/", "", $sPhoneNumber);
You can use substr to get the substring after first '-', live demo here.
substr($string, strpos($string, '-') + 1);
Please refer : Working Demo
<?php
$phonenumber = '+1-555-555-555';
$array = explode("-", $phonenumber,2);
echo $array['1'];
?>
For more details PHP EXPLODE
Try this, use preg_replace for it,
$result = preg_replace('~^[0\D]++|\D++~', '', $sPhoneNumber);
DEMO
try on this site
http://www.phpliveregex.com/p/jXB
this reg
[^\+[0-9]-].*
using this function
preg_match("/[^\+1-].*/", $input_line, $output_array);
so basic you gonna find all the is not "+1-"
Its very simple, we can use substr() function to cut portion of string.
<?php
echo substr("+1-555-555-5555",3);
?>
Here's a link PHP substr()
I want to ignore a specific character using php. So when a user adds this character in the textbox. the php scripts filters it out first. I tried something and came up with this:
<?php
$datetogoto = $_GET['datetogoto'];
$pattern = '-';
$replace = '';
preg_replace($pattern, $replace, $datetogoto);
header('Location: ../index.php?newsdate='.$datetogoto);
?>
So what it wrong with this code?
Can you try using str_replace
$datetogoto = $_GET['datetogoto'];
$datetogoto = str_replace("-","", $datetogoto);
Ref: http://us1.php.net/str_replace
Or , if you want get date format whatever you sent in query string, then use urlencode()
header('Location: ../index.php?newsdate='.urlencode($datetogoto));
PHP regex needs delimiters, so use it like this:
$pattern = '/-/';
OR else use str_replace:
str_replace('-', $replace, $datetogoto);
$str='<p>http://domain.com/1.html?u=1234576</p><p>http://domain.com/2.html?u=2345678</p><p>http://domain.com/3.html?u=3456789</p><p>http://domain.com/4.html?u=56789</p>';
$str = preg_replace('/.html\?(.*?)/','.html',$str);
echo $str;
I need get
<p>http://domain.com/1.html</p>
<p>http://domain.com/2.html</p>
<p>http://domain.com/3.html</p>
<p>http://domain.com/4.html</p>
remove ?u=*number* from every words last part. thanks.
Change this line:
$str = preg_replace('/.html\?(.*?)/','.html',$str);
into this:
$str = preg_replace('/.html\?(.*?)</','.html<',$str);
An alternative to the other answers:
preg_replace("/<p>([^?]*)\?[^<]*<\/p>/", "<p>$1</p>", $input);
This will match all types of urls with url variables, not only the ones with html-files in them.
For example, you can also extract these types of values:
<p>http://domain.com/1.php?u=1234576</p>
<p>http://domain.com?u=1234576</p>
<p>http://domain.com</p>
<p>http://domain.com/pages/users?uid=123</p>
With an output of:
<p>http://domain.com/1.php</p>
<p>http://domain.com</p>
<p>http://domain.com</p>
<p>http://domain.com/pages/users</p>
This code will load the url's into an array so they can be handled on the fly:
$str = '<p>http://domain.com/1.html?u=1234576</p><p>http://domain.com/2.html?u=2345678</p><p>http://domain.com/3.html?u=3456789</p><p>http://domain.com/4.html?u=56789</p>';
$str = str_replace("<p>","",$str);
$links = preg_split('`\?.*?</p>`', $str,-1,PREG_SPLIT_NO_EMPTY);
foreach($links as $v) {
echo "<p>".$v."</p>";
}
I try to pull out the number string from google and clean it up.
<?php
$q="35 meter in inch";
$query = explode (" ",$q);
$googleUrl="http://www.google.com/search?q=$query[0]+$query[1]+$query[2]+$query[3]";
$package = file_get_contents("$googleUrl");
$content = preg_replace('/.*<h2[^>]* style="font-size:138%"><b>|<\/b><\/h2>.*/si', "", $package) ;
$number = explode (" ",$content);
$result = str_replace(' ','',$number[3]);
echo $result;
?>
however, the number i've got has a space.
I tried to replace it with needles " " or "  ;". Or utf8_encode, decode $content. None of them works.
As for the solution to your problem, the best answer is to replace anything that is not a number or punctuation using preg_replace(); Try this:
<?php
$q="35 meter in inch";
$query = explode (" ",$q);
$googleUrl="http://www.google.com/search?q=$query[0]+$query[1]+$query[2]+$query[3]";
$package = file_get_contents("$googleUrl");
$content = preg_replace('/.*<h2[^>]* style="font-size:138%"><b>|<\/b><\/h2>.*/si', "", $package) ;
$number = explode (" ",$content);
$result = preg_replace("/[^\d.]/", '', $number[3]);
echo $result;
?>
But you may want to look into using google.com/ig/calculator. It should save a lot on bandwidth and save you having to pull a full Google Results page and replace on it: http://www.google.com/ig/calculator?hl=en&q=35%20meter%20in%20inch
<?php
$q="35 meter in inch";
$query = explode (" ",$q);
$googleUrl="http://www.google.com/ig/calculator?q=$query[0]+$query[1]+$query[2]+$query[3]";
$content = file_get_contents("$googleUrl");
preg_match("/rhs:\s\"(.*)\",error/", $content, $number);
$num = explode(" ", $number[1]);
$num = preg_replace("/[^\d.]/", '', $num[0]);
echo $num;
?>
Probably because it's not really a space, even though it looks like it. You could try replacing all \w with the regular expression.
hi the space before <?php tag it it there in your code too? then that might be giving the space check that!
This is not a space you are trying to remove, it is "à" that is not visible in browser. You can also check these things by using your php script by commandline. You can use html entities function and then replace according to that
I have written the PHP code for getting some part of a given dynamic sentence, e.g. "this is a test sentence":
substr($sentence,0,12);
I get the output:
this is a te
But i need it stop as a full word instead of splitting a word:
this is a
How can I do that, remembering that $sentence isn't a fixed string (it could be anything)?
use wordwrap
If you're using PHP4, you can simply use split:
$resultArray = split($sentence, " ");
Every element of the array will be one word. Be careful with punctuation though.
explode would be the recommended method in PHP5:
$resultArray = explode(" ", $sentence);
first. use explode on space. Then, count each part + the total assembled string and if it doesn't go over the limit you concat it onto the string with a space.
Try using explode() function.
In your case:
$expl = explode(" ",$sentence);
You'll get your sentence in an array. First word will be $expl[0], second - $expl[1] and so on. To print it out on the screen use:
$n = 10 //words to print
for ($i=0;$i<=$n;$i++) {
print $expl[$i]." ";
}
Create a function that you can re-use at any time. This will look for the last space if the given string's length is greater than the amount of characters you want to trim.
function niceTrim($str, $trimLen) {
$strLen = strlen($str);
if ($strLen > $trimLen) {
$trimStr = substr($str, 0, $trimLen);
return substr($trimStr, 0, strrpos($trimStr, ' '));
}
return $str;
}
$sentence = "this is a test sentence";
echo niceTrim($sentence, 12);
This will print
this is a
as required.
Hope this is the solution you are looking for!
this is just psudo code not php,
char[] sentence="your_sentence";
string new_constructed_sentence="";
string word="";
for(i=0;i<your_limit;i++){
character=sentence[i];
if(character==' ') {new_constructed_sentence+=word;word="";continue}
word+=character;
}
new_constructed_sentence is what you want!!!