Split php value into two parts [duplicate] - php

This question already has answers here:
PHP: Split string [duplicate]
(7 answers)
Closed 1 year ago.
I want to split variable php value into two parts.
Inside a script that I use I load variables using this code:
{$searchResults.domainName}
The value is always defined like "domain name.com"
I want to splits this into two values
Before and after the dot, so I can load the value "domain" and the value ".com"
How can I achieve that with php?

You can use PHP explode for that:
$pieces = explode(".", $domainName);
/
$pieces[0] outputs 'domain'
$pieces[1] outputs 'com'
PHP Explode Manual

Related

Get portion of URL with PHP [duplicate]

This question already has answers here:
Get parts of URL in PHP
(4 answers)
Closed 1 year ago.
I had previously been using basename to grab the last part of my URL however have noticed some issues if my URL contains parameters.
So if my URL is this:
https://www.google.com/test-page/?utm_source=test
How would I pull test-page from this?
You split it by the / delimiter, and then take the fourth item
$link = 'https://www.google.com/test-page/?utm_source=test';
$split = explode('/', $link);
if(isset($split[3]))
{
echo $split[3];
}

Can you add a variable to a file path? [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
PHP String Concatenation With Variables [duplicate]
(1 answer)
Concatenating strings retrieved from form $_Post in php
(1 answer)
php String Concatenation, Performance
(12 answers)
Concatenate HTML string and variable in PHP [closed]
(3 answers)
Closed 2 years ago.
I was wondering if you can add a variable to a file-path.
Example:
path: C:\\users\\john\\school\\hardware.properties";
I want to make a variable of the word preceding the . (dot) - in this case, "hardware" - so the user can write a name in a form and search/get another property.
You can just make it a string and add the var into it
$name = $_POST['name']
$path = "C:/.../".$name.".properties"
I just make more sure what Jasper B want to write:
In your form you have some submit input and text input.
You will get datas from inputs so you will just access them like:
$name = $_POST["name"];
After you just put $name variable to path like:
$path = "C:\users\john\school\".$name.".properties";

Remove all the word before a specific word [duplicate]

This question already has answers here:
How to Get filename from a variable (url) in php? [duplicate]
(4 answers)
Closed 4 years ago.
I have this URL and I want to remove all characters before "blog_images/" this world.
http://localhost/login/uploads/blog_images/woman4.jpg
I have tried this PHP function it removes all words but I want to remove this word also, I need to get only this characters woman4.jpg
strstr(http://localhost/login/uploads/blog_images/woman4.jpg, 'blog_images/');
You can get file name using basename function php
<?php
/* $your_file_path_along_with_file_name is your input path */
$your_file_path_along_with_file_name = "http://localhost/login/uploads/blog_images/woman4.jpg";
$original_file_name = basename($your_file_path_along_with_file_name);
echo $original_file_name //it prints your file name
?>
you can use explode like this,
$dat = explode('blog_images/','http://localhost/login/uploads/blog_images/woman4.jpg');
echo $dat[1];

Pass imploded string as a couple of parameters [duplicate]

This question already has answers here:
Call dynamic Function with unknown number of params
(3 answers)
Closed 8 years ago.
I want to do something like that:
call_user_func(array('Class', 'Method'), implode(", ", $array));
Is it possible? The imploded string contains a couple of expressions. In this shape however, if $array contains 2 elements, I get an error that Class::method() needs 2 parameters, not 1.
How can I solve this?
You want call_user_func_array instead.
call_user_func_array(array('Class', 'Method'), $array);

PHP - How can I determine the numeric sequential value of a character in the english alphabet? [duplicate]

This question already has answers here:
How to convert some character into numeric in php?
(6 answers)
Closed 9 years ago.
Without using a loop, is there a way (function) to convert, for example, the letter "c" to its numeric sequence in the alphabet (3)?
I'm trying to take a literal string - $var = "c" and apply a function to it that returns 3.
Are there any built-in PHP functions that do this? I can't find any online and would rather avoid writing the function if necessary.
Anyone know of such a conversion function?
Yup.
$alphabet_position = ord(strtoupper($character)) - ord('A');

Categories