Add method return values together - php

I'm working on a project that uses the Facebook API. Im using methods from that API to automaticly create a username. But for that I need to add the firstname and the lastname.
The way on how I retrieve the firstname and lastnames is:
$userNode->getLastName();
$userNode->getFirstName();
How do I add those return values togheter?
If I do this:
$string = $userNode->getFirstName() + $userNode->getLastName();
echo "$string";
the echo shows "0".
Thanks in advance!
Mats de Waard

The first string operators example in the manual will show you:
$string = $userNode->getFirstName() . $userNode->getLastName();

You need to make a concatenation in php
in your example use "." instead of "+"
$string = $userNode->getFirstName() . $userNode->getLastName();

Related

mask data id card number in php

I have some data resembling the following:
444444444444444
I want to add a mask to the data so it looks like this:
44.444.444.4-444.444
I tried using:
$x_data = preg_replace('~.{3}\K(?!$)~', '.', $x_data);
Which got me:
444.444.444.444.444
How do I modify my preg_replace to give me the exact output I am looking for?
You could use a capture group approach, and then connect the subcomponents according to the format you are expecting.
$x_data = "444444444444444";
echo $x_data . "\n";
$x_data = preg_replace('/^(\d{2})(\d{3})(\d{3})(\d)(\d{3})(\d{3})$/',
'$1.$2.$3.$4-$5.$6',
$x_data);
echo $x_data;
This prints:
444444444444444
44.444.444.4-444.444

Can not get the special charcter from query string value using PHP

I need one help. I need to fetch all data from query string using PHP but some special charcters like (i.e-+,- etc) are not coming. I am explaining my code below.
http://localhost/test/getmethod.php?name=Goro + Gun
Here I need to get the value assign to name using the below code.
<?php
$name=$_GET['name'];
echo $name;
?>
Here I am getting the output like Goro Gun but I need the original value i.e-Goro + Gun .Please help me to resolve this issue.
#subhra try this for this case name=Goro + Gun:
<?php
$nameArr = explode('=', $_SERVER['QUERY_STRING']);
$name = str_replace("%20", " ", $nameArr[1]);
echo $name;
?>
$_SERVER['QUERY_STRING'] - this will return you full query string

Show names separated (Getting names from database)

In my database I have one column named name and in each row of that column I'm saving names like this /Mary/Sam/Bob/Michael/. To show the values in my page I need to separate them by breaking the line in each /. Can someone help me?
If I make one echo of my column I will get /Mary/Sam/Bob/Michael/ but I want:
Mary
Sam
Bob
Michael
$names=explode('/', $dbrow['name']);
foreach($names as $aname){
echo $aname.'<br>';
}
Use the explode function with array_filter function
$database_column_string = "/Mary/Sam/Bob/Michael/";
$names = array_filter(explode('/',trim($database_column_string )));
foreach($names as $name){
echo $name;
echo "<br/>";
}
\n may not render a new line in the web browser. So it's better to replace \ with the br tag:
str_replace ("/" , "<br>" , "/Mary/Sam/Bob/Michael/");
The explode function will fix your problem but you should be aware that the way you're using your database is violating the first normal form. Each record should have the same number of attributes.
Here is a guide to the normal forms http://www.bkent.net/Doc/simple5.htm
You should redesign your database to have a table called, for example, Names(id, name)

PHP GET number query

Hi to all!
I have a query that gets the name and an id.
The results is like this :
54 - Rian Ree Barrientos
I wanted to get the number 54.
I used echo (int)$_GET['number'];
But the result is "0". How can I get the number?
You can't get it that way, you need to make two query string vars for it eg:
<a href="somepage.php?number=54&str=some_string">
Make sure that if you do so, you use the urlencode function.
Now you can use:
echo (int) $_GET['number'];
And:
echo $_GET['str'];
Otherise you can use the explode function to get the two values by specifying the - delimiter.
I think you want id from the value you get from the query
I think you want something like following
$strPrice1 = mysql_query("SELECT id, name FROM table_name where id=".$_GET['id'] );
$strPrice = mysql_fetch_array($strPrice1);
echo (int)$_strPrice['id'];
But the result is "0". How can I get the number?
Really?
When I run:
print (int)('54 - Rian Ree Barrientos') . "\n";
I get 54
(php v 5.1.6)
Maybe $_GET['number'] doesn't contain what you think.
C.

Return specific HREF attribute using Xpath query

Having a major brain freeze, I have the following chunk of code:
// Get web address
$domQuery = query_HtmlDocument($html, '//a[#class="productLink"]');
foreach($domQuery as $rtn) {
$web = $rtn->getAttribute('href');
}
Which obviously gets the entire href attribute, however I only want 1 specific attribute within the href. I.e. If the href is: /website/product1234.do?code=1234&version=1.3&somethingelse=blaah
I only want to return the variable for "version", so wish to only return "1.3" in my example. What's most efficient way to do this?
You could use parse_url and parse_str to extract that information.
Bingo! Thanks webdestroya, parse_str is exactly what I am after:
$string="/website/product1234.do?code=1234&version=1.3&somethingelse=blaah";
parse_str($string,$return);
$version = $return['version'];
echo "Version: " . $version;
Prints:
Version: 1.3

Categories