Get the image url from string - string

My string is: [slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_006.jpg" slide_desc="
How to get the part after "image=" from it?

In short, use RegEx to get the data between 2 strings. For the most part, the same Reg Expressions between different languages should work just fine.
In PHP, you would want to use a preg_match.
$string = "[slide image=\"http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_006.jpg\" slide_desc=\""
preg_match("/image=\"(.*)\"/i", $string, $results)
var_dump($results)
Update regex (a little more strict):
/image\="([^"]*)"/i
All in all, going to depend on the language being used and in what context. You can get much more advanced with the RegEx, but this is just quick & dirty.
Full PHP Code Example:
<?php
$string = '[slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uplo‌​adify/uploads/bas_006.jpg" slide_desc="<h4>Promotional Package</h4> Project description sentence" text_color="#464646" slide_horizontal="false"] [slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uplo‌​adify/uploads/bas_005.jpg" slide_desc="<h4></h4>" text_color="#464646" slide_horizontal="false"]';
preg_match_all('/image\="([^"]*)"/i', $string, $results);
foreach ($results[1] as $res):
echo 'Image URL:'.$res."\n";
endforeach;
?>

Related

Replacing a certain part of string with a pre-specified Value

I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"

Extracting substring in powershell using regex

I have a string in excel that I need to extract a substring from
This is an example of the string:
<\Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue>
I'm new to regex and powershell, but I'm trying to find a way to extract the "hostname here" portion of the string. It's variable length, so indexing won't be reliable.
since you changed the sample, the comment code i posted won't work. [grin] this will, tho ...
$InStuff = '<\Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue>'
$InStuff.Split(':')[-1].Split('<')[0].Trim()
output = hostnamehere
if you have a set of sample strings, then you likely otta post them so the code can be arranged to handle the needed variants.
If that were xml, it would be straightforward
[xml]$xml = '<Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue></Text>'
(-split $xml.text.textvalue)[1]
hostnamehere

How can I implement the java hashmap by using PHP?

I use PHP to process following input:
sam
99912222
tom
11122222
harry
12299933
sam
edward
harry
the 1st to 6th line are name and phone numbe. And the last three lines is the search query, if the name is not in the list(not have phone number,print not found), otherwise output the data. My code as follow:
<?php
$_fp = fopen("php://stdin", "r");
$list = array();
for($i = 0;$i<3;$i++){
$name = strtolower(fgets($_fp));
$phone = fgets($_fp);
$list["$name"] = $phone;
}
for($i = 0;$i<3;$i++){
$name = fgets($_fp);
if(array_key_exists($name,$list)){
echo "$name".'='."$list[$name]"."\n";
}else{
echo 'Not found'."\n";
}
?>
Excepted output should be sam = 99912222 Not found harry = 12299933
The output is sam = 99912222 Not found Not found. why these function doesn't work?
This is a problem from hackerrank.
I know if I use hashmap in java is easy to solve. But how can I solve this problem in PHP?
Many thanks
First, trim off whitespace by using trim(fgets($_fp)) everywhere instead of just fgets($_fp) -- that fixes things on my end at least.
Second, the code you pasted is missing the closing curly bracket on your second for loop.
Third, have fun with 30 Days of Code :-) (once you get the above straightened out you also need to have your code read in the number of entries at the beginning, and "Read the queries until end-of-file" at the end).

what's the best way to hardcode a multiple-line string?

In unit test I would like to hard code a block of lines as a string.
In C# I would do
var sb = new StringBuilder();
sb.AppendLine("myline1");
sb.AppendLine("myline2");
sb.AppendLine("myline3");
Since I converted to F# I tried to minimize the usage of .Net method by using bprintf instead, but somehow there is no bprintfn support which seems strange to me.
It is tedious to add \r\n at the end of each line manually.
Or is there any better way than StringBuilder?
Little known feature: you can indeed indent string content - by ending each line with a backslash. Leading spaces on the following line are stripped:
let poem = "The lesser world was daubed\n\
By a colorist of modest skill\n\
A master limned you in the finest inks\n\
And with a fresh-cut quill.\n"
You will still need to include \n or \n\r at line ends though (as done in the example above), if you want these embedded in your final string.
Edit to answer #MiloDCs question:
To use with sprintf:
let buildPoem character =
sprintf "The lesser world was daubed\n\
By a colorist of modest skill\n\
A master limned %s in the finest inks\n\
And with a fresh-cut quill.\n" character
buildPoem "you"
buildPoem "her"
buildPoem "him"
If you are under F# 3.0, triple-quoted strings may be the answer:
let x = """
myline1
myline2
myline3"""
I'm surprised nobody has mentioned this:
[ "My first line"
"second line"
"another line" ]
|> String.concat "\n"
You can create directly multi-line string literals in F#:
let multiLineStr =
"myline1
myline2
myline3"
and C#:
var multiLineStr =
#"myline1
myline2
myline3";
I think there is no problem with using StringBuilder in F# as you did.
There is a function fprintfn in Printf module, so you can use it with a StringWriter object:
let sw = new StringWriter()
fprintfn sw "myline1"
fprintfn sw "myline2"
fprintfn sw "myline3"
sw.ToString()
I like fprintf and fprintfn since they are flexible. You can write to console output by supplying stdout instead.
You could define your own bprintfn function:
let bprintfn bldr fmt =
(bldr, fmt) ||> Printf.kbprintf bldr.AppendLine
Or, to define a multi-line literal you could use triple-quotes as bytebuster suggested or a "verbatim literal," which begins with # (see Strings on MSDN).
I'm out of touch with F#, but you might be able to do adapt my normal approach:
['line1', 'line2', 'line3'].join('\n'); //javascript
StringUtils.join(Arrays.asList("line1", "line2", "line3"), "\n")); // java, using Apache Commons Lang

Drupal 6: How do you print Taxonomy Terms as a CSS Body Class?

In Drupal 6, how do you print a taxonomy term as a CSS body class?
I have found this snippet that lets you print almost every aspect of Drupal content as a body class, but it doesn't include taxonomy terms:
http://www.davidnewkerk.com/book/122
Being able to print taxonomy terms as a body class is essential for theming processes, so I am surprised that a solution is not readily available.
Check what variables are passed to the page template by either doing print_r($vars) or dpm($vars) in your page pre-process function or using the http://drupal.org/project/devel_themer module. The usage of dpm require you to install the devel module.
You will find that some themes will pass $taxonomy as a variable to page.tpl.php . If that is not the case you can find the taxonomy terms in the $node variable which is also available in the page.tpl.php in some themes.
(The above holds true for my fusion based theme acquia marina http://drupal.org/project/acquia_marina ). Once you have these taxonomy terms available you can easily print them out in your body classes.
After much hard work, I found a very easy way to do this.
On Drupal Snippets, there is a snippet that lets you print out the taxonomy terms applied to each page as text.
The only problem is that the snippet will print any spaces or punctuation that are in the taxonmy term, which is no good for body classess.
However, by adding a str_replace command, you can strip out all the spaces and punctuation.
I'm sure there are other people who wants to print taxonmy terms as body classes, so to save them the bother, here is the code that I used with the str_replace command added.
Put the following in template.php:
function getTerm($label, $vid, $link) {
$node = node_load(array('nid'=>arg(1)));
foreach((array)$node->taxonomy as $term){
if ($term->vid == $vid){
if ($link){
$link_set[] = l($term->name, taxonomy_term_path($term));
} else {
$link_set[] = $term->name;
}
}
}
if (!empty($link_set)){
$label = ($label) ? "<strong>$label </strong>" : "";
$link_set = $label.implode(', ', $link_set);
}
$link_set = str_replace(' ', '_', $link_set);
$link_set = str_replace('&', 'and', $link_set);
$link_set = strtolower($link_set);
return $link_set;
}
Put the following in Page.tpl.php:
<body class="taxonomy-<? print getTerm(false, 1, false);?>">
I hope this helps anyone who has the same problem.
Extra tips:
(1)In the code I have posted, the only punctuation that is striped out is the ampersand (i.e. '&').
If you have other punctuation to strip out use the following:
$link_set = str_replace('INSET_PUNCTUATION_HERE', 'INSERT_REPLACEMENT_HERE', $link_set);
Place this command under the other $link_set lines in the code I have posted for template.php.
(2) In the page.tpl.php code I have posted, the "taxonomy-" part places the words taxonomy and a dash before each body class term. You can edit this as you wish to get the results your require.

Resources