I have a list of web addresses such as listed below in my DB.
I need to get the domain name from each address in the list.
http://en.wordpress.com/tag/1000-things-we-hate/
http://en.wordpress.com/tag/1019/
http://en.wordpress.com/tag/1030-am/
http://www.yahoo.com/index.html
http://www.msn.com/index.html
Here's a way to do it in Java:
String input = "http://en.wordpress.com/tag/1000-things-we-hate/";
// Assuming that all urls start with "http://"
int finish = input.indexOf("/", 7);
if(finish == -1)
{
finish = input.length();
}
System.out.println(input.substring(7, finish));
Prints en.wordpress.com (I assume that is what you want?)
<?php
$url = "http://en.wordpress.com/tag/1000-things-we-hate/";
$bits = explode("/",$url);
$nextBits = explode(".",$bits[1]);
$count = count($nextBits);
$domain = $nextBits[$count-1].".".$nextBits[$count];
echo $domain;
?>
<?php
echo parse_url($url, PHP_URL_HOST);
That would return "en.wordpress.com". If you don't want subdomains (i.e. only "wordpress.com), then things are getting complicated. You would need something like http://www.dkim-reputation.org/regdom-libs/
Use the parse_url in PHP.
Related
I'm listening for connection changes through events pluging ("amq.rabbitmq.event", "connection.#").
It works properly so I'm adding at java side two additional parameters as clientproperties, to get the identity of the user that connects or disconnect.
However at c# side I can only access these properties as a list of byte[], and not sure on how to convert it to a Dictionary or so..
If I print all entries
if (args.BasicProperties.Headers.TryGetValue("client_properties", out object value))
{
var items = value as List<object>;
foreach(var item in items)
{
Console.WriteLine($"{item.GetType().ToString()}");
var bytes = item as byte[];
result.Add(Encoding.UTF8.GetString(bytes));
}
}
I can see this:
{<<"platform">>,longstr,<<"Java">>}
{<<"capabilities">>,table,[{<<"connection.blocked">>,bool,true},{<<"basic.nack">>,bool,true},{<<"exchange_exchange_bindings">>,bool,true},{<<"authentication_failure_close">>,bool,true},{<<"publisher_confirms">>,bool,true},{<<"consumer_cancel_notify">>,bool,true}]}
{<<"groupId">>,longstr,<<"1e6e935f0d4d9ec446d67dadc85cbafd10d1a095">>}
{<<"information">>,longstr,<<"Licensed under the MPL. See http://www.rabbitmq.com/">>}
{<<"version">>,longstr,<<"4.8.1">>}
{<<"copyright">>,longstr,<<"Copyright (c) 2007-2018 Pivotal Software, Inc.">>}
{<<"product">>,longstr,<<"RabbitMQ">>}
What kind of object format is and how can I parse this?:
{<<id>>,type,<<value>>}
Apparently ( as for an answer I got on Rabbit client google group for this questions ), client_properties is something that's not created to being read by the receiving party..
However is a really good way to have something like LWT ( Last Will and Testament ), then I am using it at the minute doing the parse by myself.
if (args.BasicProperties.Headers.TryGetValue("client_properties", out object value))
{
var items = value as List<object>;
foreach (var item in items)
{
var bytes = item as byte[];
//{<<id>>, type, <<value>>}
String itemStr = Encoding.UTF8.GetString(bytes);
var parts = itemStr.Split(",");
var key = CleanErlangString(parts[0]);
var value = CleanErlangString(parts[2]);
// Do things with key/value
}
}
ClearErlangFunction
private static string CleanErlangString(string toClean)
{
return toClean
.Replace("{", "").Replace("}", "")
.Replace("\"", "")
.Replace("<<", "").Replace(">>", "");
}
What I am doing to use it as LWT, is setting a custom property on client side and then obtaining it while reading events at "amq.rabbitmq.event", "connection.#". With that I know who have disconnected and even process something as LWT with my core server.
I hope this helps someone :)
I want to send a String[] by an HTTP request and get the values in PHP with the $_GET method.
The total number of values in the String[] is variable.
I have tried so far:
List<NameValuePair> params = new ArrayList<NameValuePair>();
String[] dropdowns = {"1st item","2nd item","3rd item","4th item"};
for (int i = 0; i < dropdowns.length; i++) {
params.add(new BasicNameValuePair("pid", dropdowns[i]));
}
In PHP I want to get all values and query based on them.
$pid = $_GET['pid'];
And use them like:
$result = mysql_query("SELECT *FROM Apps WHERE pid[0] = $pid" AND pid[1] = $pid"
AND ...);
But I know this way is wrong.
How can I do that?
This
$result = mysql_query("SELECT *FROM Apps WHERE pid[0] = $pid" AND pid[1] = $pid" AND ...);
Is very wrong and unsafe. (Columns wrong syntax, SQL injection, wrong quotation, wrong SQL syntax,...)
Must be something like
$result = mysql_query("
SELECT * FROM Apps WHERE pid
IN(" . implode(',', mysql_real_escape_string($pid)) . ")
");
You can create a serialized reprezentation of the values you want to send in the url. It has limitations such as the max length of the url.
'http://domain.com/data_handler.php?data=' . urlencode(serialize($array1));
Getting back your array:
$array1 = unserialize($_GET['data']);
Its even better to create a post request and use this syntax:
pid[]=1
pid[]=2
http://www.php.net/manual/en/faq.html.php
You cannot send an array through HTTP request UNLESS you have an array of inputs such as:
<input type='text' name='manyOfThese[]' />
To send an array you have two options. One is to use serialize() and unserialize() to turn your array into a string. And the other is to use session variables:
$_SESSION['pid'] = $pid;
Then on the next script
$pid = $_SESSION['pid'];
unset($_SESSION['pid']);
foreach($pid as $element){
echo $element //or do whatever you need to do to that variable
}
Also at the beginning of your scripts you will want to include:
session_start();
And then when your php application is exited (upon logoff for example):
session_destroy();
There are two parts to this and both involve loops. First, when you are sending the data, put the brackets in the name to send it as an array:
for (int i = 0; i < dropdowns.length; i++) {
params.add(new BasicNameValuePair("pid[]", dropdowns[i]));
}
Second, on the php end this array is stored in $_GET['pid'] or $_POST['pid'] depending on how you sent it, so you would loop through the array and add the items to your sql query. Just make a separate variable to store the sql statement so you can add to it:
$x = 0;
foreach($_GET['pid'] as $value) {
$yourSQLString .= " AND pid[". $x ."] = '" . $value . "'";
$x++;
}
And obviously you should do something else with the actual value to avoid sql injections.
maybe its duplicate but i cant find it.
something = new ArrayList<>();
something.add(new Object("Hello"));
something.add(new object("World"));
something.add(new Object("!"));
for(blablabla){
System.out.print(something.get(i).getTextFromConstructor());
}
this will print "Hello World!"
in php i dont know solution to pass whole objects into array to call their methods from loop or just by something[0]->method();
As i know this in php cannnot be done, but maybe i am wrong :-)
Thank You
$arr = array();
$arr[] = new MyUSerDefinedObject("Hello");
//...
echo $arr[0]->methd();
//or
foreach ($arr as $val) {
echo $val->methd();
}
Use this example to set the bridge b/w php and java so that you can pass values to them
<?php
$date = new Java("java.util.Date", 70, 9, 4);
var_dump($date->toString());
$map = new Java("java.util.HashMap");
$map->put("title", "Java Bridge!");
$map->put("when", $date);
echo $map->get("when")->toString()."\n";
echo $map->get("title")."\n";
$array = array(1,2,3,4,5);
$map->put("stuff", $array);
var_dump($map->get("stuff"))."\n";
$system = new JavaClass("java.lang.System");
echo "OS: ".$system->getProperty("os.name")."\n";
$math = new JavaClass("java.lang.Math");
echo "PI: ".$math->PI."\n";
?>
I want to add multiple product to cart from my external site.
I can add single product using this url :
http://localhost/magento/index.php/checkout/cart/add?product=2&qty=2
but for multiple I can’t use like this : http://localhost/magento/index.php/checkout/cart/add?product=2&qty=2&product=3&qty=4.
Is there any other way to do this ? My external source site is in JSP.
And I want to make connection to magento database using Java. So, Is there any tutorials or examples for Connection using Java? I know that SOAP & RPC can be used, but I don't know how to use.
Actually, I've written this code, into "CartController.php" file.
public function newAction() {
$cart = $this->_getCart();
try {
//getting list of products
$filter = new Zend_Filter_LocalizedToNormalized(array('locale' => Mage::app()->getLocale()->getLocaleCode()));
for ($i = 1; $i <= 4; $i++) {
echo '<br>';
$param = $_GET['product' . $i];
if (isset($param)) {
$param = explode("/", $param);
print_r($param);
$productId = $param[0];
$product = $this->addNewProduct($productId);
$quantity = $filter->filter($param[1]);
$params['product'] = $product;
$params['qty'] = $quantity;
$cart->addProduct($product);
}// if over
}//for over
$cart->save();
$message = $this->__('Added to Your Cart Successfully', Mage::helper('core')->escapeHtml());
$this->_getSession()->addSuccess($message);
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$this->_redirect("checkout/cart/");
} catch (Mage_Core_Exception $e) {}
Now, we can add products, like this : http://localhost/magento/index.php/checkout/cart/new?product1=2/3&product2=3/3&product3=4/1
Where, in "product1=2/3" 2=>productId & 3=> it's quantity.
This code is working properly. But,I want to do this without modifying client's code.
here is my php code
$titikPetaInti = array();
while($row = mysql_fetch_assoc($hasil2))
{
$titikPetaInti[] = $row['koordinat'];
}
$data = "{titikPeta:".json_encode($titikPetaInti)."}";
echo $data;
?>
then here is my android code
xResultTitikPeta is result request to php
jObject = new JSONObject(xResultTitikPeta);
JSONArray myArray1 = (JSONArray) jObject.getJSONArray("titikPeta");
String[]titikPeta = new String[myArray1.length()];
for(int a = 0; a < myArray1.length(); a++)
{
titikPeta[a] = myArray1.getJSONObject(a).toString();
}
teks1 = (TextView) findViewById(R.id.textView1);
teks1.setText(Arrays.toString(titikPeta));
it displaying null at emulator like no value
--EDIT--
i think there something mistake in parsing code, cus when i display the xResultTitikPeta in android, it give me string result
here is result of xResultTitikPeta
{titikPeta:["-8.705378,115.225189","-8.56056700000000,115.42395100000","-8.57659700000000,115.40065300000","-8.55596300000000,115.41085700000","-8.51855200000000,115.491908000000","-8.54743200000000,115.41036800000","-8.56551100000000,115.45173900000","-8.44321000000000,115.616019000000"]}
this is malformed JSON! no double quotes on key.
$data = "{titikPeta:".json_encode($titikPetaInti)."}";
instead do:
$data = '{"titikPeta":'.json_encode($titikPetaInti).'}';
EDITED:
Ok, remove that hand made approach:
$data = json_encode(array("titikPeta"=>$titikPetaInti));
OK, I've found your bug! As well as fixing the $data = json_encode(array("titikPeta" => $titikPetaInti)); issue, the problem is here:
titikPeta[a] = myArray1.getJSONObject(a).toString();
The elements of myArray1 are actually of type string and cause an exception to be thrown, so you need instead:
titikPeta[a] = myArray1.getString(a);
This produces the output of:
[-8.705378,115.225189, -8.56056700000000,115.42395100000, -8.57659700000000,115.40065300000, -8.55596300000000,115.41085700000, -8.51855200000000,115.491908000000, -8.54743200000000,115.41036800000, -8.56551100000000,115.45173900000, -8.44321000000000,115.616019000000]
As each element in your array is of the form "-8.705378,115.225189", the JSON parser assumes they are strings. If you change the elements to "-8.705378","115.225189" you can also use:
titikPeta[a] = Double.toString(myArray1.getDouble(a));
However, the first version will work too.
Note: my personal preference is that I would declare each array element as:
{"x":-8.705378,"y":115.225189}
try
$data = "{\"titikPeta\":".json_encode($titikPetaInti)."}";