How to make the first letter of specific array in PHP? - php

I have a simple regex that trims urls to their root domain.
Problem: How to make the first letter of a specific array in PHP ? The array output is an associative array. The line echo $matches[0] is the output that I need to convert the first letter to capitalize.
<?php
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
$url = 'http://www.test.com.uk';
//echo $url;
if (preg_match($pattern, $url, $matches) === 1) {
echo $matches[0];
}
?>
The code works okay except that the associative array must have a capitalize letter
The output of the code above looks like this: test.com.uk
Output: But the output I am looking for is this: Test.com.uk
Please help me.

Use ucfirst() on your match:
echo ucfirst($matches[0]);

Use the ucfirst function:
echo ucfirst($matches[0]);

Just use the ucfirst php function
echo ucfirst($matches[0]);

Might be more appropriate instead of regex:
echo ucfirst(parse_url($url, PHP_URL_HOST));

Related

String replace in php after "[||]"

I am getting a unique issue. I have string like "Home[||](|i am in here|)[||]". Now i want to replace everything after first occurance from left of '[||]' with null. Tried many ways like pregreplace,substring,explode,strstr,strreplace... But nothing seems to work out. Can someone please help.
Using strstr() would be about the simplest...
echo strstr("Home[||](|i am in here|)[||]", "[||]", true);
You can use regex to find the part of the string you want to replace.
<?php
$input = "Home[||](|i am in here|)[||]";
$pattern = "/\[\|\|\](.*)/";
$output = preg_replace($pattern, "", $input);
echo $output;
?>
You can do it by many way using strstr(), strtok(), preg_replace() etc. But I would prefer with explode(), without any kind of regex or str_* function to achieve what you want. Hope this will help to get your job done.
echo explode('[||]',"Home[||](|i am in here|)[||]")[0];
DEMO: https://eval.in/1040308
With strtok()
<?php
$mystring = 'Home[||](|i am in here|)[||]';
$first = strtok($mystring, '[||]');
echo $first; // Home
?>
With strstr():
<?php
$mystring = 'Home[||](|i am in here|)[||]';
$first = strstr($mystring, "[||]", true);
echo $first; // Home
?>

Getting a word after a specific character in PHP

I want to get the words in a string where a specific character stands before, in this case, its the : character.
textexjkladjladf :theword texttextext :otherword :anotherword
From this snippet the expected output will be:
theword
otherword
anotherword
How do i do this with PHP?
You can use regular expression:
$string = "textexjkladjladf :theword texttextext :otherword :anotherword";
$matches = array();
preg_match_all('/(?<=:)\w+/', $string, $matches);
foreach ($matches[0] as $item) {
echo $item."<br />";
}
Output is:
theword
otherword
anotherword
The array what you want is the $matches[0]
Another way of getting those words without Regular Expression can be:
Use explode(' ',$str) to get all the words.
Then loop the words and check which one starts with ':'.
try
$str = 'textexjkladjladf :theword texttextext :otherword :anotherword';
$tab = exlode(':',$str);
print_r($tab);
//if echo entry 0 =>
echo $tab[0]; // => textexjkladjladf

Extracting a string between an "id_" and a ".html"

I want to extract the id from this Youku video:
http://v.youku.com/v_show/id_XNTU2NzQyNzQ0.html?f=19275195&ev=3
The id is the random letter between the id_ and .html
How to accomplish that?
Use this
$input = 'http://v.youku.com/v_show/id_XNTU2NzQyNzQ0.html?f=19275195&ev=3';
preg_match('~id_(.*?).html~', $input, $output);
echo $output[1];
Output
XNTU2NzQyNzQ0
Codepad
You can try below code:
<?php
$varStr = 'http://v.youku.com/v_show/id_XNTU2NzQyNzQ0.html?f=19275195&ev=3';
$filename = basename($varStr);
preg_match_all('/id_(.*)\.html/', $filename, $match);
echo $match[1][0];
?>
Just for the sake of using named results in your REGEX, I would recommend doing something like this. Everyone else's work just fine, I've just added the named grouping as well as a non-greedy approach by ignoring periods
<?
$regex = "/\id_(?P<video_id>[^\.]*)\./";
if(preg_match($regex, "http://v.youku.com/v_show/id_XNTU2NzQyNzQ0.html?f=19275195&ev=3", $matches)) {
echo $matches['video_id'];
}

php regex how to remove duplicate charactors and make every charactor unique in string

How should one use preg_replace() to replace a string from 'aabbaacc' to 'abc'?
Currently, my code uses str_split() then array_unique() then implode().
I think preg_replace() can achieve this also, but I don't know how.
Thank you for your help.
A regex that seems to work for me is /(.)(?=.*?\1)/. Please test it for yourself here:
http://regexpal.com/
I've also tested it with preg_replace('/(.)(?=.*?\1)/', '', 'aaabbbabc') which returns the expected abc.
Hope this helps :)
This is the closest I got. However, it's basically a copy of :
How do I remove duplicate characters and keep the unique one only in Perl?
<?php
$string = 'aabbaacc';
$new = preg_replace( '/(.)(?=.*?\1)/i','', $string );
echo $new;
?>
Unfortunately, it does not keep the string in the same order. I don't know if that is important to you or not.
try this
$string = 'dbbaabbbaac';
$new = preg_replace_callback( array("/(.)\\1+/"),function($M){print_r($M);return $M[1];}, $string );
$new = preg_replace_callback( array('/(.)(.?\\1)/i','/(.)(.*?\\1)/i'),function($M){return $M[1].trim($M[2],$M[1]);}, $new );
echo $new."\n";
output
dbac
or this with out Regex
$value="aabbaacc";
for($i=0;$i<strlen($value);$i++){
$out[$value[$i]]=$value[$i];
}
echo implode("",$out);
output:
abc

how to return matches value using preg_replace()

please how do that ?
<?php
$string = '<inc="file.php">';
return preg_replace('#<inc\s*="\s*([a-zA-Z0-9\_]+)\">#',include("$1"),$string);
?>
the output is return $1
i need to return include file.php
If you have PHP 5.3 or higher:
<?php
$string = '<inc="file.php">';
return preg_replace_callback('#<inc\s*="\s*([a-zA-Z0-9\_]+)\">#', function($matches) {
return include $matches[1];
}, $string);
?>
I think it should be
return preg_replace('#<inc\s*="\s*([a-zA-Z0-9\_]+)\">#','include("'.$1.'")',$string);
The second parameter should be a string.
I'm not exactly sure what you're trying to do, but try using preg_match over preg_replace.
It gathers the matches into an array if you use it like shown:
preg_match('#<inc\s*="\s*([a-zA-Z0-9\_]+)\">#', $string, $matches);
And then the matches are stored in the $matches array. You can then loop over them using foreach.

Categories