Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create a function that returns TRUE if a string is all lowercase without using any built-in PHP functions. How can I do that?
This is what I was working with, but using ctype_lower.
$string = "string";
if (ctype_lower($string)) {
echo $string . ' is all lowercase letters.';
} else {
echo $string . ' is not all lowercase letters.';
}
Well, if you just want a simple-purpose check, you could just compare the original string with an strtolowerred string. Of course, it won't be foolproof. Like this:
function check_lowercase_string($string) {
return ($string === strtolower($string));
}
$string = 'Hi! I am a string';
var_dump(check_lowercase_string($string)); // false
var_dump(check_lowercase_string('test')); // true
A painful attempt of no functions:
function check_lowercase_string($string) {
$chars = '';
// map all small characters
for($alpha = 'a'; $alpha != 'aa'; $alpha++) { $small[] = $alpha; }
$l = 0; // not strlen() :p
while (#$string[$l] != '') {
$l++;
}
for($i = 0; $i < $l; $i++) { // for each string input piece
foreach($small as $letter) { // for each mapped letter
if($string[$i] == $letter) {
$chars .= $letter; // simple filter
}
}
}
// if they are still equal in the end then true, if they are not, false
return ($chars === $string);
}
var_dump(check_lowercase_string('teSt')); // false
You can either use ctype_lower, which is inbuilt, so you don't want that :c.
But you can use the answer from here. And put together a loop which checks for the first character. If uppercase then return true, and if not, then remove the first character of string and continue like that through the entire string.
Related
This question already has answers here:
How can I split a string in PHP at the nth occurrence of a needle?
(12 answers)
Closed 2 years ago.
I'm trying to remove all the characters of a string AFTER a character appears 4 times, in this case the fourth appearance of character "/".
So if I have a string like this:
https://www.imbd.com/zenithasianfancyfood/posts
I want to get something like:
https://www.imbd.com/zenithasianfancyfood/
Here we go, We can achieve this using explode function
<?php
$url = 'https://www.imbd.com/zenithasianfancyfood/posts';
echo get_before_url_on_particlar_slash($url,4);
function get_before_url_on_particlar_slash($url,$slash_position)
{
if(isset($url,$slash_position) && !empty($url))
{
$m = explode('/',$url);
$s = '';
foreach ($m as $k => $v)
{
if($k == $slash_position)
{
break;
}
if($k == 0)
{
$s .= $v.'//';
}
else
{
$s .= $v.'/';
}
}
}
return rtrim($s,"/");
}
?>
http://sandbox.onlinephpfunctions.com/code/a24389180a7cad014c62e9381d9caf9107c42092
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I was trying to make a URL shortener. Where the give url needs to convert to base 62
I have converted the URL in following but it convert it to random number like 0 or 1sromm8 or 2gs0ygibs
base_convert($shortener->full_url, 10, 36);
How do i convert it to base62 so that every time 5 characters unique name generated.
I use this customized function to convert String to base62 String
function b62e($str) {
if(mb_check_encoding($str, 'ASCII')) {
$out = '';
$len = strlen($str);
for($i=0; $i<$len; $i+=8) {
$chunk = substr($str, $i, 8);
$outlen = ceil( strlen($chunk)*8/6 );// 8bit/char in, 6bits/char out, round up
$hex = bin2hex($chunk);// gmp won't convert from binary, so go via hex
$raw = gmp_strval(gmp_init(ltrim($hex, '0'), 16), 62);// gmp doesn't like leading 0s
$out .= str_pad($raw, $outlen, '0', STR_PAD_LEFT);
}
return $out;
}
return false;// unicode chars not supported
}
function b62d($str) {
if(mb_check_encoding($str, 'ASCII')) {
$out = '';
$len = strlen($str);
for ($i=0; $i<$len; $i+=11) {
$chunk = substr($str, $i, 11);
$outlen = floor( strlen($chunk)*6/8 );// 6bit/char in, 8bits/char out, round down
$raw = gmp_strval(gmp_init(ltrim($chunk, '0'), 62), 16);// gmp doesn't like leading 0s
$pad = str_pad($raw, $outlen * 2, '0', STR_PAD_LEFT);// double output length as as we're going via hex (4bits/char)
$out .= pack('H*', $pad);// same as hex2bin
}
return $out;
}
return false;// unicode chars not supported
}
Demo
All credits go to Marcus Bointon, Thanks to Flash Thunder answer
May you also use obfuscator to prevent users decode variables
function obf62rep($which){// make array before translate string
$rep = [];
$str1 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$str2 = 'GHIJKLMNqrstuvwx01OPQRSTUVWXYZhijklmnop23456789abcdefgyzABCDEF';
if($which){// while obfuscate from-->to
$fr = str_split($str1);
$to = $str2;
}else{// while de-obfuscate to-->from
$fr = str_split($str2);
$to = $str1;
}
foreach($fr as $k=>$v){// make array tr-->to
$rep[$v] = $to[$k];
}
return $rep;
}
function obf62e($str){return strtr($str,obf62rep(1));}// translate string = obf
function obf62d($str){return strtr($str,obf62rep(0));}// translate back = deobf
Demo
And then make it more simple to use, like
function ob62e($str){return obf62e(b62e($str));}// encode base62 + obfuscate
function ob62d($str){return b62d(obf62d($str));}// decode base62 + deobfuscate
Sample usage
echo ob62e('test');
$name = ob62e('jhon');
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how correctly make on php ?
If set range have small letters -> replace them on big.
length of string - const
number of symbols from 0
set range : [4,6]
Example :
17xG2v9Hj5 -> 17xG2V9Hj5
b7qfK5yte9 -> b7qfK5Yte9
My code :
$m = '17xG2v9Hj5';
$s1 = mb_substr($m, 0, 4); // -> 17xG
$s2 = mb_substr($m, 4, 3); // -> 2v9
$ss = ucwords(strtoupper($s2)); // -> 2V9
$s3 = mb_substr($m, 7,3); // -> Hj5
$my = $s1.$ss.$s3; // -> 17xG2V9Hj5
var_dump($m).'<br/>';
var_dump($s1).'<br/>';
var_dump($s2).'<br/>';
var_dump($ss).'<br/>';
var_dump($s3).'<br/>';
var_dump($my).'<br/>';// ??? ['<br/>'] for [var_dump()]
// don't work;
String : [a-z] and [0-9].
It is possible to make more shortly and faster?
Thanks.
I'm really not sure what you're trying to achieve?
var_dump is typically used for debug purposes, its output will be more readable if wrapped in <pre></pre> tags.
var_dump($my).'<br/>';
Will not actually append '<br/>' to the output of var_dump it will append it the the value returned by var_dump, in this case void
ie.
$out = var_dump($my).'<br/>'; //$out == '<br/>'
If you want to output <br/> after var_dump you must echo it separately.
ie.
var_dump($my);
echo '<br/>';
You may use substr_replace to do this in one round (Assuming range and string-length are fixed)
$in = '17xG2v9Hj5';
var_dump(substr_replace($in, mb_strtoupper(mb_substr($in, 4, 3)), 4, 3));
A multibyte variant of the substr_replace function can be found here
Answer below is based on Revision 1 of the question assuming a more general solution is wanted
Naive solution but should work if I understood your question correctly:
<?php
$in = '17xG2v9Hj5';
$range = [4,6];
var_dump(uc_range($in, $range));
function uc_range($string, array $range) {
if(!is_string($string)) {
throw new InvalidArgumentException('$string is supposed to be a string');
}
$chars = str_split_unicode($string);
foreach(range($range[0], $range[1]) as $keyIndex) {
if(isset($chars[$keyIndex])) {
$chars[$keyIndex] = mb_strtoupper($chars[$keyIndex]);
}
}
return implode("", $chars);
}
// see http://www.php.net/manual/en/function.str-split.php#107658
function str_split_unicode($str, $l = 0) {
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i < $len; $i += $l) {
$ret[] = mb_substr($str, $i, $l, "UTF-8");
}
return $ret;
}
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi ,
I have a list of keywords which I need to search on the url to see if there's any match.
$keyword ='keyword1|keyword2|keyword3';
$url could be
$url = "www.keyword1castle.com";
if there is a match just echo something
How could I do this?
http://php.net/stripos
$found = false;
foreach (explode('|', $keyword) AS $val) {
$found = stripos($url, $val);
if ($found !== false) {
break;
}
}
if ($found !== false) {
echo "string found";
}
You can do something like:
explode your keyword string with | as the delimiter
loop through the array
check if the URL contains the string using strpos() (or stripos() if you want a case-insensitive check)
if it does, break out of the loop
if it doesn't, repeat the process
Code:
$keyword ='keyword1|keyword2|keyword3';
$url = "www.keyword1castle.com";
$parts = explode('|', $keyword);
foreach ($parts as $part) {
if(strpos($url, $part) !== FALSE) {
echo "keyword found in URL\n";
break;
}
else {
echo "keyword not found\n";
}
}
Documentation: explode(), strpos()
Demo!
Try this
$keyword ='keyword1';
$url = "www.keyword1castle.com";
if (strpos($url,$keyword) !== false) {
echo 'true';
}
You can use strpos : PHP.net
if (false !== strpos($url, 'keyword1')) {
echo "keyword1 found";
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP case-insensitive in_array function
I'm making a function to validate input by having an $input variable and a $whitelist variable. Both are strings and the function parses the $whitelist variable and makes it into a character array to use on the string.
For some reason the in_array function is not distinguishing a lowercase letter from an uppercase one (at least I think that's what's happening).
Link to code: http://pastebin.com/eadAV7gg
Why not match against the lowercase value of the letter. If you match it in the if statement, then you don't have to worry about changing the actual $input string:
foreach ($inputArray as $key => $value)
{
foreach ($whitelistArray as $key2 => $value2)
{
if (in_array(strtolower($value), $whitelistArray))
{
//Do nothing, check passed
}
else
{
unset($inputArray[$key]);
}
}
}
A much shorter version of what you have could be accomplished with:
$input = "ATTT";
$whitelist = "abcdefghijklmnopqrstuvwxyz";
$output = '';
for($x=0; $x<strlen($input);$x++){
if(stristr($whitelist,substr(strtolower($input),$x,1))){
$output .= substr($input,$x,1);
}
}
echo $output;
Or to implement as a function:
function whitelist_string($input=''){
$whitelist = "abcdefghijklmnopqrstuvwxyz";
$output = '';
for($x=0; $x<strlen($input);$x++){
if(stristr($whitelist,substr(strtolower($input),$x,1))){
$output .= substr($input,$x,1);
}
}
return $output;
}
echo whitelist_string('ATTT').'<br>';
echo whitelist_string('Hello88 World!').'<br>';