How can I Format the strings into specific formats [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have some SQL fields which I need to show as the heading of the table in PHP
Following are some of my fields names and I want to show them as Following strings
market_name =>Market Name
company_name => Company Name
company_address => Company Address
operating_hours => Operating hours
Is there any String Formatting option in there ?

You can combine ucwords() and str_replace() to produce the required result:
Code:
$strings = array('market_name', 'company_name', 'company_address', 'operating_hours');
foreach($strings as $string){
$string = ucwords(str_replace('_', ' ', $string));
echo $string."<br>";
}
Output:
Market Name
Company Name
Company Address
Operating Hours
Documentation: ucwords(), str_replace()
Hope this helps!

This should do the trick:
$string = "market_name";
//replace the underscore with whitespace
$step1 = str_replace("_", " ", $string);
//capitalize the words
$step2 = ucwords($step1);
//this will give you
Market Name

Related

extract two different date string between one special characters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have some problem.
I have some data from my post on date range picker
there are
"10/04/2013 - 10/26/2013"
I want to get
date1 = "10/04/2013"
and
date2 = "10/26/2013"
for my between date query..
please help me
Thank you for your attention
You need to use explode()
$string="10/04/2013 - 10/26/2013"; //Your string
$exploded = explode('-', $string); //Explode using -
echo $exploded[0]; //echo string 1
echo $exploded[1]; //echo string 2
Note: You will have to use trim() if you want to get rid of the white space, else in the explode() first parameter, use the spaces before and after the - like explode(' - ', $string)
For mysql query the date format should be as "YYYY-mm-dd"
Your input comes as MM/dd/YYYY
You may need to convert that to mysql format before firing the query
In that case you can use strtotime()
date("Y-m-d",strtotime("10/04/2013"));
will give you 2013-10-04

How to retrieve a word in a string from an array of possible words [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I hope I worded the question correctly! I want to retrieve the name of a country if it exists within a string.
$bio = 'A biography about someone from France';
$countries = ['Germany', 'Spain', 'France'];
How could I check for the existence of a country in the string using the countries array?
And then, if a match is found, return it? In this example, I'd be left with the word France.
try this code:
print_r(array_intersect(explode(' ', $bio), $countries));
or
foreach ($countries as $v)
if (mb_stripos($bio, $v) !== false)
{
echo $v;
break;
}
or
echo #array_shift(array_intersect(explode(' ', $bio), $countries));
u have to use foreach on ur countries array, and inside check if strpos(bio, country) to find the position of the country word. if the position is not false it means it's there!!

How to get movie or TV Season name from file name? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to create movie database of my movies and TV Seasons stored on my NAS. But there is a problem the files are name like this:
The.Walking.Dead.S04E01.HDTV.x264-ASAP
R.I.P.D.2013.HDRip.x264.AC3-FooKas
the.perks.of.being.a.wallflower.2012.1080p.bluray.x264-sparks
I tried to search those strings on many websites like TMDB.org but they can't find films like this. Is it possible to get a TMDB-friendly name of movie (or season) from those strings? I tried to replace dots by spaces but it didn't help. Any ideas?
I think that the following steps could work, but I'm not familiar with regular expressions:
replace dots with spaces
cut the string when the year appear
search it in the TMDB.org API
I'm afraid there are files without the year or the year is after the quality (1080p,...) or after the uploader nickname.
These names are in free format, but all has the same elements, like 'name', 'year', 'codec', 'quality', 'resolution', etc... so you can create list of there elements and filtr them out.
The goal is to get maximum readable name of film
I never wrote something on php, so I first created script on Perl and then translate it to php as I can... so forgive me some hairy php code
$arr = array(
'The.Walking.Dead.S04E01.HDTV.x264-ASAP',
'R.I.P.D.2013.HDRip.x264.AC3-FooKas',
'the.perks.of.being.a.wallflower.2012.1080p.bluray.x264-sparks'
);
foreach($arr as $value) {
$words = preg_split('/[.]/', $value);
$words = array_filter($words, create_function('$var','return !(preg_match("/(?:HDTV|bluray|\w{2,3}rip)|(?:x264)|(?:\d{4})|(?:\d{3,4}p)|(?:AC\d)/i", $var));'));
echo join(' ', $words);
echo "\n";
}
The output will be:
The Walking Dead S04E01
R I P D
the perks of being a wallflower
this is close enought... If something wrong with my code here is Perl's code, I sure it works:
my #data = (
'The.Walking.Dead.S04E01.HDTV.x264-ASAP',
'R.I.P.D.2013.HDRip.x264.AC3-FooKas',
'the.perks.of.being.a.wallflower.2012.1080p.bluray.x264-sparks'
);
for my $el ( #data ) {
my #words = grep
{!/(?:HDTV|bluray|\w{2,3}rip)|(?:x264)|(?:\d{4})|(?:\d{3,4}p)|(?:AC\d)/i}
split /[.]/, $el;
print join ' ', #words, "\n";
}
This should at least get you started. I'm sure there are better ways to do this, and I'm sure you're going to find places where it needs to be tweaked to match all scenarios.
$subject = "the.perks.of.being.a.wallflower.2012.1080p.bluray.x264-sparks";
$pattern = '/[a-zA-Z0-9\.]+\.[0-9]{4}\./';
preg_match($pattern, $subject, $matches);
echo substr(str_replace('.', ' ', $matches[0]), 0, -6);

Trim string after and before a character [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to trim this string after "=" and before ";"
com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;
so that it echoes 8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75
You can use the str_replace function to replace multiple values by, in this case, nothing:
str_replace(array('com=', ';'), '', $string);
This can also work:
parse_str(trim(
'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;'
, ';'));
echo "$com\n";
Try this:
$value="aaaacom=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;";`
$pos=strpos($value,"=");
echo substr($value,$pos+1,strlen($value)-($pos+2));
You can use a regex:
$str = 'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;';
echo preg_replace('/([a-z]+)=(.*);/', '\2', $str);
But it looks like a .ini file, then maybe you want this:
Try this:
$ini = 'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;';
$parsed_ini = parse_ini_string($ini);
echo $parsed_ini['com'];

Regular Expression,Regex PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to validade a string in the following format:
numbers, letters and _
Minimum length 4 and max length 15
At least 1 letter [a-z]
For example:
Valid:
ABCD
ABCDE
ABC_
01A_
A12345_BCDW1234
Not Valid:
ABC
01A
A12345_BCDW123411
_1212392034
_
A_1
I made a couple tries but none work.
I don't think you can do this in just one regex, the validation of constraints on length {4,15} and "must contains a letter" must be done independently.
$test_inputs = array(
'ABCD', 'ABCDE', 'ABC_', '01A_', 'A12345_BCDW1234'
, 'ABC', '01A', 'A12345_BCDW123411', '_1212392034', '_', 'A_1'
);
$res = array();
foreach($test_inputs as $input)
{
$res[$input] = (preg_match('/^[A-Z0-9_]{4,15}$/i', $input) && preg_match('/[A-Z]/i', $input));
}
var_dump($res);

Categories