I am try to use explode() on a string fetched from a database but it didn't work. I have tried explode('-',$string) but it's still not working.
Here is my string which I want to explode:
Expression of Interest – Join our Paint Team – North
If you look closely the hyphen from the string is not the same as the hyphen you use as your argument for the explode.
The hyphen in the string is the following – while the hyphen you pass as the argument for explode() is -. As you can see they don't match (the one in the string is longer than the one you try to compare it with). Because the characters don't match, the explode function is returning the whole string.
<?php
$string = "Expression of Interest – Join our Paint Team – North";
$strings = explode('–', $string);
var_dump($strings);
I have copied the hyphen from the text and used that as an argument for explode() and it works fine.
Might be $string is not string, you can use strval( $string ) to convert it to string i.e. explode('–', strval ( $string ) );
$eString = explode('–', strval ( $string ) );
// vardump($eString) - Now it is an array.
// echo $eString[0];
I have fix the issue by trying this
$post_job_title = htmlentities(get_the_title($posts));
$post_job_title = explode (" – ", $post_job_title);
Related
i want to trim a single word i.e, Monday in wordpress, how can i trim this word?
$my_title = get_the_title();
echo wp_trim_words($my_title, 1, null );
the title coming from database Monday So i want to trim Monday to Mo or M.
wp_trim_words() will works easily without any problems. You can use it with below sample code:
echo wp_trim_words( get_the_title(), 1, '' );
But do note that it will trim the first character in whole post/page title, not regconize the date in your example.
I don't know that wp_trim_words() works that way. I'd recommend just using regex and preg_match():
$my_title = get_the_title();
to get the first character:
$regex = '/(.?)/';
or to get the first two characters, change the variable to this:
$regex = '/(.?)./';
Then get the matches for the regex pattern:
preg_match($regex, $my_title, $matches);
Echo out the first one:
echo $matches[0];
I need to search those elements who have space " " in their attributes.
For example:
<unit href="http:xxxx/unit/2 ">
Suppose above code have space in the last for href attribute.
I have done this using FLOWER query. But I need this to be done using CTS functions. Please suggest.
For FLOWER query I have tried this:
let $x := (
for $d in doc()
order by $d//id
return
for $attribute in data($d//#href)
return
if (fn:contains($attribute," ")) then
<td>{(concat( "id = " , $d//id) ,", data =", $attribute)}</td>
else ()
)
return <tr>{$x}</tr>
This is working fine.
For CTS I have tried
let $query :=
cts:element-attribute-value-query(xs:QName("methodology"),
xs:QName("href"),
xs:string(" "),
"wildcarded")
let $search := cts:search(doc(), $query)
return fn:count($search)
Your query is looking for " " to be the entirety of the value of the attribute. If you want to look for attributes that contain a space, then you need to use wildcards. However, since there is no indexing of whitespace except for exact value queries (which are by definition not wildcarded), you are not going to get a lot of index support for that query, so you'll need to run this as a filtered search (which you have in your code above) with a lot of false positives.
You may be better off creating a string range index on the attribute and doing value-match on that.
How can I make Java print "Hello"?
When I type System.out.print("Hello"); the output will be Hello. What I am looking for is "Hello" with the quotes("").
System.out.print("\"Hello\"");
The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include:
Carriage return and newline: "\r" and "\n"
Backslash: "\\"
Single quote: "\'"
Horizontal tab and form feed: "\t" and "\f"
The complete list of Java string and character literal escapes may be found in the section 3.10.6 of the JLS.
It is also worth noting that you can include arbitrary Unicode characters in your source code using Unicode escape sequences of the form \uxxxx where the xs are hexadecimal digits. However, these are different from ordinary string and character escapes in that you can use them anywhere in a Java program ... not just in string and character literals; see JLS sections 3.1, 3.2 and 3.3 for a details on the use of Unicode in Java source code.
See also:
The Oracle Java Tutorial: Numbers and Strings - Characters
In Java, is there a way to write a string literal without having to escape quotes? (Answer: No)
char ch='"';
System.out.println(ch + "String" + ch);
Or
System.out.println('"' + "ASHISH" + '"');
Escape double-quotes in your string: "\"Hello\""
More on the topic (check 'Escape Sequences' part)
You can do it using a unicode character also
System.out.print('\u0022' + "Hello" + '\u0022');
Adding the actual quote characters is only a tiny fraction of the problem; once you have done that, you are likely to face the real problem: what happens if the string already contains quotes, or line feeds, or other unprintable characters?
The following method will take care of everything:
public static String escapeForJava( String value, boolean quote )
{
StringBuilder builder = new StringBuilder();
if( quote )
builder.append( "\"" );
for( char c : value.toCharArray() )
{
if( c == '\'' )
builder.append( "\\'" );
else if ( c == '\"' )
builder.append( "\\\"" );
else if( c == '\r' )
builder.append( "\\r" );
else if( c == '\n' )
builder.append( "\\n" );
else if( c == '\t' )
builder.append( "\\t" );
else if( c < 32 || c >= 127 )
builder.append( String.format( "\\u%04x", (int)c ) );
else
builder.append( c );
}
if( quote )
builder.append( "\"" );
return builder.toString();
}
System.out.println("\"Hello\"");
System.out.println("\"Hello\"")
There are two easy methods:
Use backslash \ before double quotes.
Use two single quotes instead of double quotes like '' instead of "
For example:
System.out.println("\"Hello\"");
System.out.println("''Hello''");
Take note, there are a few certain things to take note when running backslashes with specific characters.
System.out.println("Hello\\\");
The output above will be:
Hello\
System.out.println(" Hello\" ");
The output above will be:
Hello"
Use Escape sequence.
\"Hello\"
This will print "Hello".
you can use json serialization utils to quote a java String.
like this:
public class Test{
public static String quote(String a){
return JSON.toJsonString(a)
}
}
if input is:hello output will be: "hello"
if you want to implement the function by self:
it maybe like this:
public static String quotes(String origin) {
// 所有的 \ -> \\ 用正则表达为: \\ => \\\\" 再用双引号quote起来: \\\\ ==> \\\\\\\\"
origin = origin.replaceAll("\\\\", "\\\\\\\\");
// " -> \" regExt: \" => \\\" quote to param: \\\" ==> \\\\\\\"
origin = origin.replaceAll("\"", "\\\\\\\"");
// carriage return: -> \n \\\n
origin = origin.replaceAll("\\n", "\\\\\\n");
// tab -> \t
origin = origin.replaceAll("\\t", "\\\\\\t");
return origin;
}
the above implementation will quote escape character in string but exclude
the " at the start and end.
the above implementation is incomplete. if other escape character you need , you can add to it.
I have several John Jones with different names in a form. I submit the form to another program which uses "$checkednames = implode(', ', $_POST['raters']);". I echo $checkednames and see all the names but "count($checkednames)" is 1 and not the number of names. What could be wrong?
I appreciate any help.
You may seen the results by going to:
www.golfcourseratingassistant.org/ratecourse/
select Course name > Select Tee Box > Course Data ...select for all lists then "Save Data".
Selected data is only valid for the current session.
It will be 1 only, implode() returns a string containing a string representation of all the array elements in the same order, with the glue string between each element. See Manual
explode() will return an array of strings
So count() after explode() will give you the number of elements.
You can see the names because it is one string.
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo count($comma_separated);// Output will be 1
For explode():
$string= "lastname,email,phone";
$array= explode(",", $string);
echo count($array); //output will be 3
Hello I have a project where I need to cApitalize only the second letter in a sentence. I now that PHP has strtoupper() and string strtoupper ( string $string ) ucfirst() returns first letter
So here is my best attempt
<?php
$str = "capitalize";
$str = ucfirst(strtolower($str)); // makes all the letters lower case
?>
This is where I get confused if 0 = the first letter and 1= 2nd then could I just make an array(") or count_chars() then $val
Its a old question, just came across this so would put an answer based on #doppelgreener comment.
This should work :
$str = "capitalize";
$str[1]= strtoupper($str[1]);
echo $str; // cApitalize
i have one idea to perform this operation..
example
$strmain='capitalize';
$result = substr($strmain, 0, 1); //result is c
$result1=str_replace($result,'',$strmain);//now your result1 is apitalize
$result2=ucfirst($result1); //now result2 is Apitalize
$finalresult=$result.$result2 ///now your finalresult is cApitalize